Skip to content

Commit

Permalink
Add new "Search by image" context menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
bijij committed Dec 30, 2021
1 parent a59ce8b commit d0e4378
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
38 changes: 38 additions & 0 deletions js/background.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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)}"`
})
}
});
}
}
);
2 changes: 1 addition & 1 deletion js/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']) {
Expand Down
7 changes: 6 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand All @@ -22,8 +22,13 @@
}
},
"permissions": [
"contextMenus",
"storage"
],
"optional_permissions": [
"tabs",
"*://*/*"
],
"options_ui": {
"page": "html/options.html"
},
Expand Down

0 comments on commit d0e4378

Please sign in to comment.