Skip to content

Commit

Permalink
Merge pull request #2 from Mastercuber/master
Browse files Browse the repository at this point in the history
[WIP] Refactor background, increase version, remove permission
  • Loading branch information
adbar committed Jun 16, 2023
2 parents 96d04de + c45a98c commit efb8cc8
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 51 deletions.
3 changes: 3 additions & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Contributors of the DWDS.org Browser Add-On
* Adrien Barbaresi
* Armin Kunkel
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
19 changes: 19 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -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
123 changes: 80 additions & 43 deletions src/background.js
Original file line number Diff line number Diff line change
@@ -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
}
});
})
44 changes: 44 additions & 0 deletions src/manifest-chrome.json
Original file line number Diff line number Diff line change
@@ -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"

}
12 changes: 5 additions & 7 deletions src/manifest.json
Original file line number Diff line number Diff line change
@@ -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"
}
},

Expand All @@ -30,16 +29,15 @@

"permissions": [
"menus",
"search",
"https://www.dwds.de/"
"search"
],

"chrome_settings_overrides": {
"search_provider": {
"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"
}
Expand Down

0 comments on commit efb8cc8

Please sign in to comment.