diff --git a/AUTHORS.md b/AUTHORS.md new file mode 100644 index 0000000..5ac9e2c --- /dev/null +++ b/AUTHORS.md @@ -0,0 +1,3 @@ +Contributors of the DWDS.org Browser Add-On +* Adrien Barbaresi +* Armin Kunkel \ No newline at end of file diff --git a/README.md b/README.md index 6421104..232570f 100644 --- a/README.md +++ b/README.md @@ -8,4 +8,9 @@ This extension allows for - forwarding selected text to the DWDS search page on right-click - typing dwds in the address bar to trigger a link suggestion corresponding to user input -Firefox add-on page: [https://addons.mozilla.org/firefox/addon/dwds/](https://addons.mozilla.org/firefox/addon/dwds/) +* Firefox add-on page: [https://addons.mozilla.org/firefox/addon/dwds/](https://addons.mozilla.org/firefox/addon/dwds/) +* Chrome Webstore search: [https://chrome.google.com/webstore/category/extensions](https://chrome.google.com/webstore/category/extensions) + + +[Publish to Webstore](https://developer.chrome.com/docs/webstore/publish/) +[Why manifest v3](https://developer.chrome.com/docs/extensions/mv2/) \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..5a7c138 --- /dev/null +++ b/build.sh @@ -0,0 +1,19 @@ +#!/bin/bash +set -xe + +cd src +rm -Rf web-ext-artifacts/ +version=$(grep -E "\"version\": \"" manifest.json | grep -o "[0-9]*\.[0-9]*\.[0-9]*") + +mv manifest-chrome.json ../manifest-chrome.json +web-ext build +mv web-ext-artifacts/im_dwds_nachschlagen-$version.zip ../im_dwds_nachschlagen-$version-firefox.zip + +mv manifest.json ../manifest-firefox.json +mv ../manifest-chrome.json manifest.json + +web-ext build +mv web-ext-artifacts/im_dwds_nachschlagen-$version.zip ../im_dwds_nachschlagen-$version-chrome.zip + +mv manifest.json manifest-chrome.json +mv ../manifest-firefox.json manifest.json \ No newline at end of file diff --git a/src/background.js b/src/background.js index 08485b8..5c27599 100644 --- a/src/background.js +++ b/src/background.js @@ -1,71 +1,108 @@ +/** + * Some functionality can be used the same way with both API's! + * Firefox and Safari: browser + * Chrome, Opera, Edge: chrome + * See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Build_a_cross_browser_extension + */ +const isFirefox = (typeof browser === 'object') +const pluginApi = (isFirefox ? browser : chrome) +const contextMenu = isFirefox ? browser.menus : chrome.contextMenus + // display text according to current locale -var menuText = browser.i18n.getMessage("menuContent"); -var omniboxSuggestion = browser.i18n.getMessage("omniboxSuggestion"); -var omniboxText = browser.i18n.getMessage("omniboxText"); +const menuText = pluginApi.i18n.getMessage("menuContent") +const omniboxSuggestion = pluginApi.i18n.getMessage("omniboxSuggestion") +const omniboxText = pluginApi.i18n.getMessage("omniboxText") // construct queries starting from this URL -const baseURL = "https://www.dwds.de/?q="; - +const baseURL = "https://www.dwds.de/?q=" +const suggestionBaseURL = "https://www.dwds.de/wb/typeahead?q=" /* -Create a menu item for the search engine -*/ -function createMenuItem(engines) { - browser.menus.create({ + Actions to execute only when the add-on is installed or updated + See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/onInstalled + */ +pluginApi.runtime.onInstalled.addListener(() => { + /* + Create a contextmenu entry and tell the browser to show this entry only when text is selected. + See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/menus/create + */ + contextMenu.create({ id: "dwds", title: menuText, contexts: ["selection"] - }); -} - -browser.search.get().then(createMenuItem); + }) +}) /* -Search using the search engine whose name matches the -menu item's ID. +When the contextmenu item is clicked: +In Firefox -> search using the search engine with name "DWDS" declared in the manifest +In Chrome -> create a new tab with the URL of DWDS (since in Chrome no engine can be selected) +See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/menus/onClicked */ -browser.menus.onClicked.addListener((info, tab) => { - browser.search.search({ - query: info.selectionText, - engine: "DWDS" - }); -}); +contextMenu.onClicked.addListener((info, tab) => { + if (info.menuItemId === 'dwds') { + if (isFirefox) { + browser.search.search({ + query: info.selectionText, + engine: "DWDS" + }) + } else { + chrome.tabs.create({url: baseURL + info.selectionText}) + } + } +}) /* -Use omnibox to trigger DWDS query on user's request +Set the default suggestion description +See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/omnibox/setDefaultSuggestion */ -browser.omnibox.setDefaultSuggestion({ +pluginApi.omnibox.setDefaultSuggestion({ description: omniboxSuggestion -}); +}) -function getSuggestions(input) { - var result = []; - let suggestion = { - content: baseURL + input, - description: omniboxText - } - result.push(suggestion); - return result; +async function getSuggestions(input) { + const result = [] + await fetch(suggestionBaseURL + input) + .then(res => res.json()) + .then(res => { + for (const suggestion of res) { + result.push({ + content: baseURL + suggestion.value, + description: `${suggestion.value}` + }) + } + }).catch(console.error) + + return result } -browser.omnibox.onInputChanged.addListener((input, suggest) => { - suggest(getSuggestions(input)); -}); +/* +See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/omnibox/onInputChanged + */ +pluginApi.omnibox.onInputChanged.addListener(async (input, suggest) => { + suggest(await getSuggestions(input)) +}) -browser.omnibox.onInputEntered.addListener((url, disposition) => { +/* +See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/omnibox/onInputEntered + */ +pluginApi.omnibox.onInputEntered.addListener((url, disposition) => { + if (!url.startsWith(baseURL)) { + url = baseURL + url + } switch (disposition) { case "currentTab": - browser.tabs.update({url}); - break; + pluginApi.tabs.update({url}) + break case "newForegroundTab": - browser.tabs.create({url}); - break; + pluginApi.tabs.create({url}) + break case "newBackgroundTab": - browser.tabs.create({url, active: false}); - break; + pluginApi.tabs.create({url, active: false}) + break } -}); +}) diff --git a/src/manifest-chrome.json b/src/manifest-chrome.json new file mode 100644 index 0000000..08dac95 --- /dev/null +++ b/src/manifest-chrome.json @@ -0,0 +1,44 @@ +{ + + "manifest_version": 3, + "name": "__MSG_extensionName__", + "description": "__MSG_extensionDescription__", + "version": "0.2.0", + + "default_locale": "de", + + "icons": { + "16": "icons/favicon-16.png", + "32": "icons/favicon-32.png", + "48": "icons/favicon-48.png", + "64": "icons/icon-64.png", + "96": "icons/icon-96.png", + "128": "icons/icon-128.png" + }, + + "omnibox": { "keyword" : "dwds" }, + + "background": { + "service_worker": "background.js" + }, + + "permissions": [ + "contextMenus", + "search" + ], + + "chrome_settings_overrides": { + "search_provider": { + "name": "DWDS", + "search_url": "https://www.dwds.de/?q={searchTerms}", + "keyword": "dwds", + "favicon_url": "/icons/favicon-32.png", + "is_default": false, + "encoding": "UTF-8" + } + }, + + "author": "Adrien Barbaresi", + "homepage_url": "https://github.com/zentrum-lexikographie/dwds-addon" + +} \ No newline at end of file diff --git a/src/manifest.json b/src/manifest.json index 0e26f40..443deab 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -1,15 +1,14 @@ { - "manifest_version": 2, + "manifest_version": 3, "name": "__MSG_extensionName__", "description": "__MSG_extensionDescription__", - "version": "0.1.4", - + "version": "0.2.0", "default_locale": "de", "browser_specific_settings": { "gecko": { - "strict_min_version": "63.0" + "id": "browser-addon@dwds.de" } }, @@ -30,8 +29,7 @@ "permissions": [ "menus", - "search", - "https://www.dwds.de/" + "search" ], "chrome_settings_overrides": { @@ -39,7 +37,7 @@ "name": "DWDS", "search_url": "https://www.dwds.de/?q={searchTerms}", "keyword": "dwds", - "favicon_url": "https://www.dwds.de/favicon-32x32.png", + "favicon_url": "/icons/favicon-32.png", "is_default": false, "encoding": "UTF-8" }