Add-to-Semantic-Scuttle-WebExt/background.js

77 lines
2.0 KiB
JavaScript
Raw Normal View History

function onError(error) {
console.log(`Error: ${error}`);
}
function onGot(item) {
instance = item["instance_url"];
windowWidth = item["window_width"];
windowHeight = item["window_height"];
noQueryStrings = item["remove_querystrings"];
}
function shareURL(donnees){
2017-07-14 05:11:06 +00:00
browser.tabs.query({active: true},function(tabs){
browser.storage.local.get(["instance_url","window_width","window_height","remove_querystrings"],function(item){
2017-08-07 16:25:13 +00:00
let instance = item["instance_url"];
let windowWidth = item["window_width"];
let windowHeight = item["window_height"];
let noQueryStrings = item["remove_querystrings"];
2017-07-14 05:11:06 +00:00
2017-08-07 16:25:13 +00:00
let tab = tabs[0];
// manages Mozilla Firefox reader mode
2017-08-07 16:25:13 +00:00
let rawUrl = tab.url;
let partToRemove = "about:reader?url=";
if(rawUrl.includes(partToRemove)) {
rawUrl = rawUrl.substring(partToRemove.length);
rawUrl = decodeURIComponent(rawUrl);
2017-07-22 17:52:06 +00:00
}
// manages URL query strings
if (noQueryStrings == true) {
rawUrl = rawUrl.split("?")[0];
}
2017-08-07 16:25:13 +00:00
let url = instance + "/bookmarks.php?action=add&address=" + encodeURIComponent(rawUrl) + "&title=" + encodeURIComponent(tabs[0].title) + "&description=" + encodeURIComponent(donnees);
2017-07-14 05:11:06 +00:00
widthInt = Number(windowWidth);
heightInt = Number(windowHeight);
browser.windows.create({
url: url,
width: widthInt,
height: heightInt,
type: "popup"
},(win)=>{
browser.tabs.onUpdated.addListener((tabId,changeInfo) =>{
if(tabId === win.tabs[0].id){
if(changeInfo.url){
var new_url
new_url = changeInfo.url
if((new_url.includes("action=add") == false) && (new_url.includes("edit.php") == false)){
2017-07-14 05:11:06 +00:00
browser.windows.remove(win.id);
}
}
}
});
});
});
});
2017-07-14 05:11:06 +00:00
}
browser.contextMenus.create({
id: "semantic-scuttle",
title: "Add to (Semantic)Scuttle",
onclick: function(){
shareURL();
},
contexts: ["all"]
});
browser.browserAction.onClicked.addListener((tab) => {
browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => {
shareURL(response.response);
}).catch(onError);
2017-07-14 05:11:06 +00:00
});