diff --git a/TODO b/TODO index 1f3459c..858bb8f 100644 --- a/TODO +++ b/TODO @@ -1,2 +1 @@ -Add keyboard shortcut if possible --Bug corrections & optimizations diff --git a/background.js b/background.js index 992bf0a..e7b4e7d 100644 --- a/background.js +++ b/background.js @@ -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); }); diff --git a/content-script.js b/content-script.js new file mode 100644 index 0000000..429e760 --- /dev/null +++ b/content-script.js @@ -0,0 +1,7 @@ +browser.runtime.onMessage.addListener(request => { + if (request.method == "getSelection") { + return Promise.resolve({response: window.getSelection().toString()}); + } else { + return Promise.resolve({}); + } +}); diff --git a/manifest.json b/manifest.json index 24f1b87..f982408 100644 --- a/manifest.json +++ b/manifest.json @@ -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": [""], + "js": ["content-script.js"] + } + ], "background" : { "scripts" : ["background.js"] } - }