diff --git a/README.md b/README.md index 6597f08..14f3f53 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ A chrome extension that lets you add the current tab or a note to your [Shaarli] Available on the [Chrome Web Store](https://chrome.google.com/webstore/detail/add-to-shaarli/jhfblapoehcfajokolimghdfmeeakbee). +## Features + +- **Click the extension icon** to add the current tab. +- **Right-click context menu** to easily add the current page or a link. +- **Keyboard shortcuts** for quick access. + ## Keyboard shortcuts | Key combination | Action | diff --git a/addNote.js b/addNote.js new file mode 100644 index 0000000..28a1a0e --- /dev/null +++ b/addNote.js @@ -0,0 +1,15 @@ +!function(){ + chrome.storage.sync.get({ + savedShaarliInstance: "https://example.com/" + }, function (items) { + const shaarliInstance = items.savedShaarliInstance; + const popup = window.open( + shaarliInstance + + "?post=" + + "&description=" + + encodeURIComponent(document.getSelection()), + "_blank", + "menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1" + ); + }); +}(); diff --git a/addUrl.js b/addUrl.js new file mode 100644 index 0000000..8282600 --- /dev/null +++ b/addUrl.js @@ -0,0 +1,21 @@ +!function(){ + chrome.storage.sync.get({ + savedShaarliInstance: "https://example.com/" + }, function (items) { + const shaarliInstance = items.savedShaarliInstance; + const o = location.href; + const e = document.title || o; + const popup = window.open( + shaarliInstance + + "?post=" + + encodeURIComponent(o) + + "&title=" + + encodeURIComponent(e) + + "&description=" + + encodeURIComponent(document.getSelection()) + + "&source=bookmarklet", + "_blank", + "menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1" + ); + }); +}(); diff --git a/background.js b/background.js index 88dd547..2b14bc9 100644 --- a/background.js +++ b/background.js @@ -1,52 +1,91 @@ const defaultShaarliInstance = "https://example.com/"; -function addUrl() { - chrome.storage.sync.get({ - savedShaarliInstance: defaultShaarliInstance - }, function (items) { - let shaarliInstance = items.savedShaarliInstance; - let myCode = "!function(){var o=location.href,e=document.title||o;const popup=window.open('" + - shaarliInstance + - "?post='+encodeURIComponent(o)+'&title='+encodeURIComponent(e)+'&description='+encodeURIComponent(document.getSelection())+'&source=bookmarklet','_blank','menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1')}();"; - executeScript(shaarliInstance, myCode); +chrome.runtime.onInstalled.addListener(function (details) { + if (details.reason === "install") { + chrome.runtime.openOptionsPage(); + } + chrome.contextMenus.create({ + id: "add-to-shaarli", + title: "Add to Shaarli", + contexts: ["page", "selection", "link"] }); -} +}); -function addNote() { - chrome.storage.sync.get({ - savedShaarliInstance: defaultShaarliInstance - }, function (items) { - let shaarliInstance = items.savedShaarliInstance; - let myCode = "!function(){var e=document.title;const popup=window.open('" + - shaarliInstance + - "?post='+'&description='+encodeURIComponent(document.getSelection()),'_blank','menubar=no,height=800,width=600,toolbar=no,scrollbars=yes,status=no,dialog=1')}();"; - executeScript(shaarliInstance, myCode); - }); +chrome.contextMenus.onClicked.addListener((info, tab) => { + if (info.menuItemId === "add-to-shaarli") { + addUrl(tab); + } +}); + +function addUrl(tab) { + chrome.storage.sync.get( + { + savedShaarliInstance: defaultShaarliInstance, + }, + function (items) { + const shaarliInstance = items.savedShaarliInstance; + if ( + shaarliInstance === defaultShaarliInstance || + shaarliInstance === "" + ) { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: () => { + alert( + "Please setup your Shaarli instance in the options of the extension." + ); + }, + }); + } else { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + files: ["addUrl.js"], + }); + } + } + ); } -function executeScript(shaarliInstance, code) { - if (shaarliInstance === defaultShaarliInstance || shaarliInstance === "") - code = "alert('Please setup your Shaarli instance in the options of the extension.')"; - chrome.tabs.query({ - active: true - }, function () { - chrome.tabs.executeScript({ - code: code - }); - }); +function addNote(tab) { + chrome.storage.sync.get( + { + savedShaarliInstance: defaultShaarliInstance, + }, + function (items) { + const shaarliInstance = items.savedShaarliInstance; + if ( + shaarliInstance === defaultShaarliInstance || + shaarliInstance === "" + ) { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: () => { + alert( + "Please setup your Shaarli instance in the options of the extension." + ); + }, + }); + } else { + chrome.scripting.executeScript({ + target: { tabId: tab.id }, + files: ["addNote.js"], + }); + } + } + ); } -chrome.commands.onCommand.addListener(function (command) { +chrome.commands.onCommand.addListener(function (command, tab) { switch (command) { case "add-url": - addUrl(); + addUrl(tab); break; case "add-note": - addNote(); + addNote(tab); break; } }); -chrome.browserAction.onClicked.addListener(function () { - addUrl(); -}); \ No newline at end of file +chrome.action.onClicked.addListener(function (tab) { + addUrl(tab); +}); diff --git a/manifest.json b/manifest.json index 9a9a787..b9f27e2 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "Add to Shaarli", "description": "This extension lets you add the current tab or a note to your Shaarli instance.", "version": "1.1.0", @@ -8,24 +8,21 @@ "48": "icon48.png", "128": "icon128.png" }, - "browser_action": { + "action": { "default_icon": "icon.png" }, - "options_page": "options.html", - "options_ui": { - "page": "options.html", - "open_in_tab": false - }, "background": { - "scripts": [ - "background.js" - ], - "persistent": false + "service_worker": "background.js" }, "permissions": [ + "storage", "activeTab", - "storage" + "scripting", + "contextMenus" + ], + "host_permissions": [ + "" ], "commands": { "add-url": { @@ -40,5 +37,8 @@ }, "description": "Add a note" } + }, + "content_security_policy": { + "extension_pages": "script-src 'self'; object-src 'self'" } -} \ No newline at end of file +}