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

80 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-07-14 05:11:06 +00:00
var instance
var windowWidth
var windowHeight
var noQueryStrings
2017-07-14 05:11:06 +00:00
// prefs are not loaded once before shareURL it won't work on first click.
var gettingPrefs = browser.storage.local.get(["instance_url","window_width","window_height","remove_querystrings"]);
gettingPrefs.then(onGot,onError);
2017-07-14 05:11:06 +00:00
function shareURL(){
browser.tabs.query({active: true},function(tabs){
// prefs are loaded again when the function is called to manage changes in the options page.
let gettingPrefs = browser.storage.local.get(["instance_url","window_width","window_height","remove_querystrings"]);
gettingPrefs.then(onGot,onError);
2017-07-14 05:11:06 +00:00
var tab = tabs[0];
// manages Mozilla Firefox reader mode
var rawUrl = tab.url;
2017-07-22 17:52:06 +00:00
var 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];
}
var url = instance + "/bookmarks.php?action=add&address=" + encodeURIComponent(rawUrl) + "&title=" + encodeURIComponent(tabs[0].title);
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);
}
}
}
});
});
});
}
function onError(error) {
console.log(`Error: ${error}`);
}
2017-07-22 17:47:25 +00:00
function onGot(item) {
instance = item["instance_url"];
windowWidth = item["window_width"];
windowHeight = item["window_height"];
noQueryStrings = item["remove_querystrings"];
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(() => {
shareURL();
});