135 lines
3.8 KiB
JavaScript
135 lines
3.8 KiB
JavaScript
/*
|
|
* Some silly variables for window dressing etc.
|
|
*/
|
|
var myWindowId;
|
|
const contentBox = document.querySelector("#content");
|
|
//var array = new Uint32Array(10);
|
|
//window.crypto.getRandomValues(array);
|
|
|
|
const cryptoObject = window.crypto.subtle;
|
|
const encoder = new TextEncoder();
|
|
const data = encoder.encode("loremipsum");
|
|
const hash = cryptoObject.digest("SHA-256", data).then(onSuccess,onError);
|
|
|
|
/*
|
|
Make the content box editable as soon as the user mouses over the sidebar.
|
|
*/
|
|
window.addEventListener("mouseover", () => {
|
|
contentBox.setAttribute("contenteditable", true);
|
|
});
|
|
|
|
/*
|
|
When the user mouses out, save the current contents of the box.
|
|
|
|
window.addEventListener("mouseout", () => {
|
|
contentBox.setAttribute("contenteditable", false);
|
|
browser.tabs.query({windowId: myWindowId, active: true}).then((tabs) => {
|
|
let contentToStore = {};
|
|
contentToStore[tabs[0].url] = contentBox.textContent;
|
|
browser.storage.local.set(contentToStore);
|
|
});
|
|
});*/
|
|
|
|
/*
|
|
Press S - store text to file
|
|
*/
|
|
window.addEventListener("keydown", function(event){
|
|
if(event.code == "KeyS"){
|
|
let querying = browser.tabs.query({currentWindow: true, active: true});
|
|
|
|
//var selection = window.getSelection();
|
|
var selection = document.getSelection();
|
|
alert("Selection: " + selection.toString());
|
|
|
|
querying.then(addToDatabase, onError);
|
|
}
|
|
if(event.code =="KeyD"){
|
|
console.log("Deleting all.");
|
|
//removeAllFromDatabase();
|
|
alert("Deleting all records from local.storage.");
|
|
}
|
|
if(event.code =="ControlLeft"){
|
|
alert("Control left pressed.");
|
|
}
|
|
else{
|
|
alert("No letter key pressed man. ");
|
|
}
|
|
});
|
|
|
|
function onError(error) {
|
|
console.log("Error: ${error}");
|
|
}
|
|
|
|
async function addToDatabase(tabs){
|
|
var webpage = {};
|
|
let tabURL = tabs[0].url;
|
|
let dateObject = new Date();
|
|
let cettimestamp = dateObject.getTime();
|
|
const encoder = new TextEncoder();
|
|
let data = encoder.encode(tabURL);
|
|
let hash = (await window.crypto.subtle.digest("SHA-256", data).then(onSuccess, onError));
|
|
let arr = {url: tabURL, sha256: hash, timestamp: cettimestamp, notes: "Non-DW site."};
|
|
/*
|
|
* Add domain from URL to have an umbrella.
|
|
*/
|
|
|
|
webpage[hash]= arr;
|
|
browser.storage.local.set(webpage).then(setItem, onError);
|
|
//browser.storage.sync.set(webpage).then(setItem, onError);
|
|
console.log("Local storage filled with " + tabURL + " and key hash " + hash);
|
|
}
|
|
|
|
function getFromDatabase(){
|
|
browser.storage.local.get("").then(receivedFromDatabase, onError);
|
|
};
|
|
|
|
function removeAllFromDatabase(){
|
|
browser.storage.local.clear().then(setItem, onError);
|
|
}
|
|
|
|
function receivedFromDatabase(item){
|
|
console.log(item);
|
|
alert(item);// + item.year + item.population);
|
|
};
|
|
|
|
function setItem(){
|
|
alert("Item inserted to local storage.");
|
|
};
|
|
|
|
function onSuccess(hashInput){
|
|
const hashArray = Array.from(new Uint8Array(hashInput));
|
|
const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('').toString();
|
|
console.log("SHA256 key: " + hashHex);
|
|
return hashHex;
|
|
};
|
|
|
|
function onError(error){
|
|
alert("Now there is an error with hashing. Ignoring.");
|
|
};
|
|
|
|
function updateContent(){
|
|
browser.tabs.query({windowId: myWindowId, active: true}).then((tabs) => {return browser.storage.local.get(tabs[0].url);}).then((storedInfo) => {
|
|
contentBox.textContent = storedInfo[Object.keys(storedInfo)[0]];
|
|
//console.log("STORedInfOi: " + storedInfo[0]);
|
|
});
|
|
};
|
|
|
|
/*
|
|
Update content when a new tab becomes active.
|
|
*/
|
|
browser.tabs.onActivated.addListener(updateContent);
|
|
|
|
/*
|
|
Update content when a new page is loaded into a tab.
|
|
*/
|
|
browser.tabs.onUpdated.addListener(updateContent);
|
|
|
|
/*
|
|
When the sidebar loads, get the ID of its window,
|
|
and update its content.
|
|
*/
|
|
browser.windows.getCurrent({populate: true}).then((windowInfo) => {
|
|
myWindowId = windowInfo.id;
|
|
updateContent();
|
|
});
|