diff --git a/js/background.js b/js/background.js index ad020e0..5b68589 100644 --- a/js/background.js +++ b/js/background.js @@ -1,3 +1,11 @@ +const DEBUG = false; + +function toI18n(str) { + return str.replace(/__MSG_(\w+)__/g, function (match, v1) { + return v1 ? chrome.i18n.getMessage(v1) : ''; + }); +} + // Default options const defaultOptions = { 'open-in-new-tab': true, @@ -14,3 +22,33 @@ const defaultOptions = { chrome.storage.sync.get('defaultOptions', function () { chrome.storage.sync.set({ defaultOptions }); }); + +// Setup "Search by image" context menu item +chrome.contextMenus.create( + { + "id": "ViewImage-SearchByImage", + "title": toI18n("__MSG_searchImage__"), + "contexts": ["image"], + } +); + +chrome.contextMenus.onClicked.addListener( + (info, tab) => { + + if (DEBUG) + console.log("ViewImage: Search By Image context menu item clicked.", info, tab); + + if (info.menuItemId === "ViewImage-SearchByImage") { + chrome.permissions.request({ + permissions: ["tabs"], + origins: [tab.url], + }, (granted) => { + if (granted) { + chrome.tabs.executeScript(tab.id, { + code: `window.location.href = "http://www.google.com/searchbyimage?image_url=${encodeURIComponent(info.srcUrl)}"` + }) + } + }); + } + } +); \ No newline at end of file diff --git a/js/content-script.js b/js/content-script.js index c936acc..1a95383 100644 --- a/js/content-script.js +++ b/js/content-script.js @@ -239,7 +239,7 @@ function addSearchImageButton(container, imageURL, version) { } // Set the search by image button url - searchImageButton.href = '/searchbyimage?image_url=' + imageURL; + searchImageButton.href = '/searchbyimage?image_url=' + encodeURIComponent(imageURL); // Set additional options if (options['open-search-by-in-new-tab']) { diff --git a/manifest.json b/manifest.json index 2585022..160d8c2 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "__MSG_appName__", - "version": "3.5.0", + "version": "3.6.0", "description": "__MSG_appDesc__", "default_locale": "en", "icons": { @@ -22,8 +22,13 @@ } }, "permissions": [ + "contextMenus", "storage" ], + "optional_permissions": [ + "tabs", + "*://*/*" + ], "options_ui": { "page": "html/options.html" },