-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Mastercuber/master
[WIP] Refactor background, increase version, remove permission
- Loading branch information
Showing
6 changed files
with
157 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "[email protected]" | ||
} | ||
}, | ||
|
||
|
@@ -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" | ||
} | ||
|