This code performs saving and sorting innerHTMLtext of body by count of times you pressed combination of keys. It works, but at some point of time script stops working at all and it need to be changed or file that contain it has to be renamed to continue working. Besides I tryed add it as Chrome extension and in debug mode I see that script connected (it output conlose.log after DOMContentLoaded event handler), but it skips some blocks of code and after every fix different one. You can see this phenomenon in both "local server" and "browser extension" program modes. I tried somehow work out this problem, looked through a lot of forums and solutions and still dont have a solution. So it's sad that I cant end up my first mini project on native js(. Sincerely wish for your guidence.
popup.js
document.addEventListener("DOMContentLoaded", function() {
let inputEl = document.body;
inputEl.addEventListener("keydown", copy_paste);
});
console.log('It connected');
let storage = {};
function copy_paste(e) {
if (e.ctrlKey && e.key == "q") {
let inputValue = window.getSelection();
navigator.clipboard.writeText(inputValue).then(
navigator.clipboard.readText().then((text) => {
if (!storage.hasOwnProperty(text)) storage[text] = 1;
else storage[text] += 1;
mySort(storage);
})
);
}
}
function show(arrStorage) {
let parent = document.getElementById("insert");
parent.innerHTML = "";
for (let i = 0; i < arrStorage.length; i++) {
let para = document.createElement("h4");
para.innerText = arrStorage[i][0] + " : " + arrStorage[i][1];
parent.appendChild(para);
}
}
function mySort(storage) {
let arrStorage = [];
for (let key in storage) {
arrStorage.push([key, storage[key]]);
}
arrStorage.sort(function (a, b) {
return b[1] - a[1];
});
show(arrStorage);
}