187 lines
5.3 KiB
JavaScript
187 lines
5.3 KiB
JavaScript
/* Licence: Mozilla Public License Version 2.0
|
|
* Icons: Book by Mike Rowe from the Noun Project,
|
|
* Justice by Creative Stall from the Noun Project
|
|
* Author: Jesper Bergman (jesperbe@dsv.su.se)
|
|
*/
|
|
|
|
/* Start by importing functions */
|
|
import {modifyDatabaseEntry, saveHighlightedText, syncWithServer, syncCategories} from './auto_store.js';
|
|
|
|
function onCreated(){
|
|
if (browser.runtime.lastError) {
|
|
console.log(`Error: ${browser.runtime.lastError}`);
|
|
} else {
|
|
console.log("Background script started succesfully.");
|
|
let categories = browser.storage.local.get("category").then((categoriesArray) => {pickOut(categoriesArray);});
|
|
}
|
|
}
|
|
|
|
async function pickOut(categoryArray){
|
|
console.log("|--- CategoryArray: ", categoryArray);
|
|
var categories = categoryArray["category"];
|
|
if(categories == undefined){
|
|
syncCategories();
|
|
}
|
|
}
|
|
|
|
/* Called when the item has been removed. We'll just log success here. */
|
|
function onRemoved() {
|
|
console.log("Item removed successfully");
|
|
}
|
|
|
|
/*function getCategories(){
|
|
var test = browser.storage.local.get("category").then((categoriesArray) => {console.log("Fetching categories from local.storage.", categoriesArray["category"]);});
|
|
var cat = categoriesArray["category"];
|
|
var cat1 = cat[2];
|
|
var cat2 = cat[0];
|
|
var cat3 = cat[1];
|
|
var categories = cat1.concat("\n", cat2, "\n", cat3);
|
|
console.log("|-- In this strange getCategories function.");
|
|
return categories;
|
|
}*/
|
|
|
|
browser.contextMenus.create({
|
|
id: "log-selection",
|
|
title: "D3-Annotator Menu",
|
|
contexts: ["selection"]
|
|
}, onCreated);
|
|
|
|
browser.menus.create({
|
|
id: "separator-1",
|
|
type: "separator",
|
|
contexts: ["all"]
|
|
}, onCreated);
|
|
|
|
// Save highlighted alias/username.
|
|
// If the title contains an ampersand "&"
|
|
// then the next character will be used as
|
|
// an access key for the item, and the ampersand will not be displayed.
|
|
browser.menus.create({
|
|
id: "highlightedText",
|
|
type: "radio",
|
|
title: "&Save Highlighted Text",
|
|
contexts: ["selection"],
|
|
checked: true,
|
|
icons: {
|
|
"16": "icons/16.png",
|
|
"32": "icons/32.png"
|
|
}
|
|
}, onCreated);
|
|
|
|
// Save highlighted free text
|
|
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/menus/create
|
|
browser.menus.create({
|
|
id: "annotateAndCategorise",
|
|
type: "radio",
|
|
title: "Annotate and Categorise Page",
|
|
contexts: ["all"],
|
|
checked: true,
|
|
icons: {
|
|
"16": "icons/16.png",
|
|
"32": "icons/32.png"
|
|
}
|
|
}, onCreated);
|
|
|
|
// Offer the opportunity to create new categories
|
|
browser.menus.create({
|
|
id: "newcategory",
|
|
type: "radio",
|
|
title: "Create new Category",
|
|
contexts: ["all"],
|
|
checked: true,
|
|
icons: {
|
|
"16": "icons/16.png",
|
|
"32": "icons/32.png"
|
|
}
|
|
}, onCreated);
|
|
|
|
browser.menus.create({
|
|
id: "separator-2",
|
|
type: "separator",
|
|
contexts: ["all"]
|
|
}, onCreated);
|
|
|
|
/* Open sidebar menu entry
|
|
browser.menus.create({
|
|
id: "open-sidebar",
|
|
title: "Open the sidebar",
|
|
contexts: ["all"],
|
|
command: "_execute_sidebar_action"
|
|
}, onCreated);
|
|
|
|
/*More sidebar menu stuff
|
|
browser.menus.create({
|
|
id: "tools-menu",
|
|
title: "Toools menu",
|
|
contexts: ["tools_menu"],
|
|
}, onCreated);*/
|
|
|
|
var checkedState = true;
|
|
|
|
function onError(error){
|
|
console.log("Error in background.js - ", error);
|
|
}
|
|
|
|
function updateCheckUncheck(){
|
|
checkedState = !checkedState;
|
|
if (checkedState) {
|
|
browser.menus.update("check-uncheck", {
|
|
title: browser.i18n.getMessage("menuItemUncheckMe"),
|
|
});
|
|
} else {
|
|
browser.menus.update("check-uncheck", {
|
|
title: browser.i18n.getMessage("menuItemCheckMe"),
|
|
});
|
|
}
|
|
}
|
|
|
|
// Get the existing categories
|
|
function getExistingCategories(item){
|
|
var categories = item;
|
|
console.log("in getCategories now. must be fulfilled: ", categories);
|
|
return categories;
|
|
}
|
|
|
|
browser.menus.onClicked.addListener((info, tab) => {
|
|
switch (info.menuItemId){
|
|
case "log-selection":
|
|
console.log(info.selectionText);
|
|
break;
|
|
case "remove-me":
|
|
var removing = browser.menus.remove(info.menuItemId);
|
|
removing.then(onRemoved, onError);
|
|
break;
|
|
|
|
case "newcategory":
|
|
let category_popup = browser.windows.create({type:"detached_panel", titlePreface:"Add a new DWA Category", url:"popup/add_category.html", width:250, height:200});
|
|
break;
|
|
/*
|
|
* Save the page to a category and/or save an annotation to the page.
|
|
*/
|
|
case "annotateAndCategorise":
|
|
/* Save highlighted text in some way with URLs in save_to_category.js
|
|
Open popup to display categories and annotation options */
|
|
if(info.selctionText != ""){
|
|
saveHighlightedText(info.selectionText);
|
|
console.log("Annotation command triggered from menu, but sending clipboard to saveHighlightedText function as well.");
|
|
}
|
|
|
|
let save_popup = browser.windows.create({type:"detached_panel", titlePreface:"Save to category", url:"popup/save_to_category.html", width:550, height:620});
|
|
|
|
//modifyDatabaseEntry("highlighted", info.selectionText);
|
|
break;
|
|
/*
|
|
* Save the marked text to local.storage DB.
|
|
*/
|
|
case "highlightedText":
|
|
saveHighlightedText(info.selectionText);
|
|
browser.notifications.create({
|
|
"type": "basic",
|
|
"iconUrl": browser.extension.getURL("icons/48.png"),
|
|
"title": "The following text was stored to the DB",
|
|
"message": info.selectionText
|
|
});
|
|
break;
|
|
}
|
|
});
|