Skip to content

Instantly share code, notes, and snippets.

@kms70847
Last active November 19, 2019 16:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kms70847/8b86b8b10148d437cb10 to your computer and use it in GitHub Desktop.
Save kms70847/8b86b8b10148d437cb10 to your computer and use it in GitHub Desktop.
Adds persistent user notes to Stack Overflow chat
// ==UserScript==
// @name User Notes (SO Chat)
// @namespace .
// @include http://chat.stackoverflow.com/rooms/*
// @include https://chat.stackoverflow.com/rooms/*
// @version 2
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
function makeArea(user_id){
try{
var data = GM_getValue(user_id, "(No notes)");
var container = document.createElement("span");
container.className = "userscript_added usernotes";
var textContainer = document.createElement("span");
textContainer.innerHTML = data.replace("\n", "<br/>");
container.appendChild(textContainer);
var textEditor = document.createElement("textarea");
container.appendChild(textEditor);
container.appendChild(document.createElement("br"));
var editButton = document.createElement("a");
editButton.innerHTML = "(edit)";
container.appendChild(editButton);
var saveButton = document.createElement("a");
saveButton.innerHTML = "(save)";
container.appendChild(saveButton);
var discardButton = document.createElement("a");
discardButton.innerHTML = "(discard)";
container.appendChild(discardButton);
function setMode(mode){
if(mode == "view"){
var viewStyle = "inline";
var editStyle = "none";
}
else if(mode == "edit"){
var viewStyle = "none";
var editStyle = "inline";
}
else{
console.log("Unrecognized mode " + mode);
}
textContainer.style.display = viewStyle;
editButton.style.display = viewStyle;
textEditor.style.display = editStyle;
saveButton.style.display = editStyle;
discardButton.style.display = editStyle;
}
editButton.onclick = function(){
setMode("edit");
textEditor.value = data;
}
saveButton.onclick = function(){
data = textEditor.value;
GM_setValue(user_id, data);
textContainer.innerHTML = data.replace("\n", "<br/>");
setMode("view");
}
discardButton.onclick = function(){
setMode("view");
}
setMode("view");
return container;
}
catch(e){
console.log(e);
}
}
function modify_popups(){
try{
var boxes = document.getElementsByClassName("popup user-popup");
if (boxes.length > 0){
var box = boxes[0];
if(box.getElementsByClassName("usernotes").length > 0){
//already added box to this popup
return;
}
var links = box.getElementsByTagName("A");
if (links.length > 0){
var user_id = links[0].href.split("/")[4];
box.insertBefore(makeArea(user_id), box.getElementsByTagName("BR")[0].nextSibling);
}
}
}
catch(e){
console.log(e);
}
}
try{
setInterval(modify_popups, 500);
}
catch(e){
console.log(e);
}
@ztane
Copy link

ztane commented Nov 19, 2019

https://...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment