Bug correction & new feature : selection is copied to description field

This commit is contained in:
Laurent Espitallier 2017-08-07 18:23:51 +02:00
parent 5edda2b0d6
commit 5f1e39f123
4 changed files with 41 additions and 28 deletions

1
TODO
View File

@ -1,2 +1 @@
-Add keyboard shortcut if possible
-Bug corrections & optimizations

View File

@ -1,18 +1,28 @@
var instance
var windowWidth
var windowHeight
var noQueryStrings
var instance;
var windowWidth;
var windowHeight;
var noQueryStrings;
// 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);
function onError(error) {
console.log(`Error: ${error}`);
}
function shareURL(){
function onGot(item) {
instance = item["instance_url"];
windowWidth = item["window_width"];
windowHeight = item["window_height"];
noQueryStrings = item["remove_querystrings"];
}
function shareURL(donnees){
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);
browser.storage.local.get(["instance_url","window_width","window_height","remove_querystrings"],function(item){
instance = item["instance_url"];
windowWidth = item["window_width"];
windowHeight = item["window_height"];
noQueryStrings = item["remove_querystrings"];
var tab = tabs[0];
@ -29,7 +39,7 @@ function shareURL(){
rawUrl = rawUrl.split("?")[0];
}
var url = instance + "/bookmarks.php?action=add&address=" + encodeURIComponent(rawUrl) + "&title=" + encodeURIComponent(tabs[0].title);
var url = instance + "/bookmarks.php?action=add&address=" + encodeURIComponent(rawUrl) + "&title=" + encodeURIComponent(tabs[0].title) + "&description=" + encodeURIComponent(donnees);
widthInt = Number(windowWidth);
heightInt = Number(windowHeight);
@ -52,17 +62,7 @@ function shareURL(){
});
});
});
}
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"];
});
}
browser.contextMenus.create({
@ -74,6 +74,8 @@ browser.contextMenus.create({
contexts: ["all"]
});
browser.browserAction.onClicked.addListener(() => {
shareURL();
browser.browserAction.onClicked.addListener((tab) => {
browser.tabs.sendMessage(tab.id, {method: "getSelection"}).then(response => {
shareURL(response.response);
}).catch(onError);
});

7
content-script.js Normal file
View File

@ -0,0 +1,7 @@
browser.runtime.onMessage.addListener(request => {
if (request.method == "getSelection") {
return Promise.resolve({response: window.getSelection().toString()});
} else {
return Promise.resolve({});
}
});

View File

@ -1,7 +1,7 @@
{
"manifest_version" : 2,
"name": "Add to (Semantic)Scuttle",
"version" : "0.6",
"version" : "0.7",
"description" : "Add bookmarks to a (Semantic)Scuttle instance, a social bookmarking tool experimenting with tags and collaborative tag descriptions.",
"homepage_url" : "https://addons.mozilla.org/fr/firefox/addon/add-to-semantic-scuttle/",
"icons" : {
@ -27,8 +27,13 @@
"default_title" : "Add to (Semantic)Scuttle",
"default_icon" : "data/icon48.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content-script.js"]
}
],
"background" : {
"scripts" : ["background.js"]
}
}