1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
|
var xnote = ""; // k b\nbbbb\nbbbbbbbbbbbböklllll11111111222222222222222333333333llllcbvvv42vvvvvv";
var xnoteOrig = "";
var dateOrig = "";
var btnState = 0;
async function onStorageChanged(changes, area) {
if (changes["xnote"]) startup();
};
messenger.storage.local.onChanged.addListener(onStorageChanged);
//console.log("loading mdisplay script");
async function startup() {
// let message = messenger.runtime.sendMessage({ command: "getXNote" });
//message.then(
// debugger;
// let gettingPage = browser.runtime.getBackgroundPage();
// console.log("xn", gettingPage.xnote);
let {xnote} = await messenger.storage.local.get("xnote");
let preferenceCache = (await browser.storage.local.get("preferences")).preferences;
xnote.show_full_in_messageDisplay = preferenceCache["show_full_in_messageDisplay"];
// console.log("stor", xnote);
notify (xnote);
let notificationDetails = await browser.runtime.sendMessage({
command: "getNotificationDetails"
});
// console.log("mdispl msg", notificationDetails);
/* notify(message);
*/
}
startup();
async function onStorageChanged(changes, area) {
if (changes["xnote"]) startup();
};
messenger.storage.local.onChanged.addListener(onStorageChanged);
//console.log("loading mdisplay script");
function notify(message) {
try {
let old = document.getElementById("xnote_msgDisplay");
old.remove();
}
catch (e) { }
function truncate(fullStr, strLen, separator) {
if (fullStr.length <= strLen) return fullStr;
// acknowledgement to https://gist.github.com/ugurozpinar/10263513
separator = separator || '...';
var sepLen = separator.length,
charsToShow = strLen - sepLen,
frontChars = Math.ceil(charsToShow / 2),
backChars = Math.floor(charsToShow / 2);
return fullStr.substr(0, frontChars) + separator + fullStr.substr(fullStr.length - backChars);
}
xnoteOrig = xnote = message.text;
dateOrig = message.date;
if (xnote.length > 0) {
let no_linebreak = xnote.replace(/(\r\n|\n|\r)/gm, " ");
let no_double_space = no_linebreak.replace(/\s+/g, " ");
let trunc = truncate(no_double_space, 350, "...");
let imgMax = document.createElement("img");
imgMax.src = messenger.runtime.getURL("icons/iconfinder_maximize-2_2561250MIT16.png");
// https://ourcodeworld.com/articles/read/376/how-to-strip-html-from-a-string-extract-only-text-content-in-javascript
let strippedHtml = "";
strippedHtml = trunc.replace(/<[^>]+>/g, ''); //html entities are not converted, <> are stripped
//document.documentElement.firstChild.appendChild(img);
let text3 = "<div id = 'xnote_msgDisplay' style = 'width:100%;background-color: #FBFEBF;' >" +
"<button id = 'chgSize'><img id = 'aa' height = '12' src = '" + imgMax.src + "' /></button><b>XNote " + message.date + ": </b><span id = 'xnoteFull'></span><span id = 'xnoteShort'>" + strippedHtml + "</span></div>";
document.documentElement.firstChild.insertAdjacentHTML("beforebegin", text3);
let btn = document.getElementById("chgSize");
btn.addEventListener("click", showAll, false);
//debugger;
if (message.show_full_in_messageDisplay) showAll();
}
}
function showAll() {
// console.log("all", xnoteOrig);
let img = document.getElementById("aa");
img.src = messenger.runtime.getURL("icons/iconfinder_minimize-2_2561246MIT16.png");
// btn.textContent = "Compact";
if (btnState == 0) {
let note = document.getElementById("xnoteShort");
note.textContent = "";//remove();
let div = document.createElement("div");
div.setAttribute("id", "fullNoteDiv");
note.append(div);
let span = document.getElementById("xnoteFull");
// let fullnote = "<div id = 'fullNote'>" + xnote + "</div>";
//span.insertAdjacentHTML("afterend",fullnote);
let brNote = xnoteOrig.replace(/(?:\r\n|\r|\n)/g, '<br />');//xnoteOrig.replace("\r", "<br>");
div.insertAdjacentHTML("afterend", brNote);
btnState = 1;
}
else {
btnState = 0;
this.value = "Show all";
let all = document.getElementById("xnote_msgDisplay");
all.remove();
notify({ text: xnoteOrig, date: dateOrig });
}
}
|