-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
30 lines (25 loc) · 928 Bytes
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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') {
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?client=firefox-b-d&image_url=' + sanitizedImageUrl,
active: !result.openInBackground,
openerTabId: tab.id
});
});
});