-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sanitize image URL; set
openerTabId
instead of index
when creatin…
…g the search tab
- Loading branch information
Showing
4 changed files
with
45 additions
and
30 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
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,23 +1,30 @@ | ||
'use strict'; | ||
|
||
// Create the context menu item | ||
browser.menus.create({ | ||
id: 'action-search', | ||
title: browser.i18n.getMessage('actionSearchImage'), | ||
contexts: ['image'] | ||
}); | ||
|
||
// Add a listener for the context menu item | ||
browser.menus.onClicked.addListener(function(info, tab) { | ||
if (info.menuItemId === 'action-search') { | ||
const getOpenInBackgroundPref = browser.storage.sync.get({ | ||
openInBackground: true | ||
}); | ||
|
||
getOpenInBackgroundPref.then(function(result) { | ||
browser.tabs.create({ | ||
url: 'https://www.google.com/searchbyimage?image_url=' + info.srcUrl, | ||
active: !result.openInBackground, | ||
index: tab.index + 1 | ||
}); | ||
}); | ||
if (info.menuItemId !== 'action-search') { | ||
return; | ||
} | ||
|
||
// URL encode '?' and '&' in the original image's URL | ||
const sanitizedImageUrl = info.srcUrl.replace(/\?|&/g, match => match === '?' ? '%3F' : '%26'); | ||
|
||
// Get the open in background option, defaulting to 'true' if it doesn't exist | ||
const getOpenInBackgroundPref = browser.storage.sync.get({ openInBackground: true }); | ||
|
||
// Create the search tab | ||
getOpenInBackgroundPref.then(function(result) { | ||
browser.tabs.create({ | ||
url: 'https://www.google.com/searchbyimage?image_url=' + sanitizedImageUrl, | ||
active: !result.openInBackground, | ||
openerTabId: tab.id | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,28 +1,31 @@ | ||
'use strict'; | ||
|
||
function saveOptions() { | ||
browser.storage.sync.set({ | ||
openInBackground: document.getElementById('input-open-background').checked | ||
function init() { | ||
// Get localized text | ||
document.getElementById('lbl-open-background').textContent = browser.i18n.getMessage('lblOpenInBackground'); | ||
|
||
// Get the open in background option, defaulting to 'true' if it doesn't exist | ||
const getOpenInBackgroundPref = browser.storage.sync.get({ openInBackground: true }); | ||
|
||
// Set the preference's initial state | ||
getOpenInBackgroundPref.then(function(result) { | ||
document.getElementById('input-open-background').checked = result.openInBackground; | ||
}); | ||
} | ||
|
||
function restoreOptions() { | ||
const setCurrentChoice = function(result) { | ||
document.getElementById('input-open-background').checked = result.openInBackground; | ||
}; | ||
|
||
document.getElementById('lbl-open-background').textContent = browser.i18n.getMessage('lblOpenInBackground'); | ||
|
||
const getOpenInBackgroundPref = browser.storage.sync.get({ | ||
openInBackground: true | ||
}); | ||
|
||
getOpenInBackgroundPref.then(setCurrentChoice); | ||
function saveOptions() { | ||
const isOpenInBackgroundPrefChecked = document.getElementById('input-open-background').checked; | ||
browser.storage.sync.set({ openInBackground: isOpenInBackgroundPrefChecked }); | ||
} | ||
|
||
document.addEventListener('DOMContentLoaded', restoreOptions); | ||
document.addEventListener('DOMContentLoaded', init); | ||
document.getElementById('input-open-background').addEventListener('change', saveOptions); | ||
|
||
if (!['en', 'pt', 'es', 'ru', 'uk'].includes(browser.i18n.getUILanguage().substring(0, 2))) { | ||
const SUPPORTED_LANGUAGES = ['en', 'pt', 'es', 'ru', 'uk']; | ||
const browserLang = browser.i18n.getUILanguage().substring(0, 2); | ||
|
||
// Show message inviting the user to contribute with translations, | ||
// but only if their browser language is not supported by the extension | ||
if (!SUPPORTED_LANGUAGES.includes(browserLang)) { | ||
document.getElementById('msg-translate').removeAttribute('style'); | ||
} |