Skip to content

Commit 45d4804

Browse files
committed
v3.0.0:
* It's now possible to select more than one search engine (fixes #6) * * If only one search engine is selected, no submenu is shown (old behaviour) * Fixed a bug where default settings were restored, when "open in background" was not checked and the add-on was disabled and re-enabled
1 parent 823f5d6 commit 45d4804

File tree

6 files changed

+218
-80
lines changed

6 files changed

+218
-80
lines changed

_locales/de/messages.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
"searchProviderLabel": {
3939
"message": "Suchanbieter"
4040
},
41-
42-
"other": {
43-
"message": "Anderer"
44-
},
45-
41+
4642
"customSearchProviderLabel": {
4743
"message": "Eigener Suchanbieter"
4844
},
4945

46+
"searchProvidersError": {
47+
"message": "Bitte wähle mindestens eine Suchmaschine!"
48+
},
49+
5050
"customSearchProviderPlaceholder": {
5151
"message": "Gib eine URL ein und nutze %s als Platzhalter für die Bilder-URL..."
5252
},
@@ -61,5 +61,9 @@
6161

6262
"saved": {
6363
"message": "Gespeichert!"
64+
},
65+
66+
"error": {
67+
"message": "Fehler, Einstellungen wurden nicht gespeichert!"
6468
}
6569
}

_locales/en/messages.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@
3838
"searchProviderLabel": {
3939
"message": "Search provider"
4040
},
41-
42-
"other": {
43-
"message": "Other"
44-
},
45-
41+
4642
"customSearchProviderLabel": {
4743
"message": "Custom search provider"
4844
},
4945

46+
"searchProvidersError": {
47+
"message": "Please choose at least one search engine!"
48+
},
49+
5050
"customSearchProviderPlaceholder": {
5151
"message": "Type a URL and use %s as placeholder for the image URL..."
5252
},
@@ -61,5 +61,9 @@
6161

6262
"saved": {
6363
"message": "Saved!"
64+
},
65+
66+
"error": {
67+
"message": "Error, settings were not saved!"
6468
}
6569
}

background.js

Lines changed: 60 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,51 @@
11
// Set up context menu for images
2-
var title = chrome.i18n.getMessage("contextMenuTitle");
3-
chrome.contextMenus.create({
4-
id: "343642_image-reverse-search",
5-
title: title,
6-
contexts: ["image"]
7-
});
2+
function createContextMenu(options) {
3+
const title = chrome.i18n.getMessage("contextMenuTitle");
4+
5+
/* If there is only one search provider, do not create a submenu */
6+
if (options.searchProviders.length == 1) {
7+
chrome.contextMenus.create({
8+
id: options.searchProviders[0],
9+
title: title,
10+
contexts: ["image"]
11+
});
12+
return
13+
}
14+
15+
/* Create menu and submenu entries */
16+
chrome.contextMenus.create({
17+
id: "Image-Reverse-Search",
18+
title: title,
19+
contexts: ["image"]
20+
});
21+
for (i = 0; i < options.searchProviders.length; i++) {
22+
chrome.contextMenus.create({
23+
parentId: "Image-Reverse-Search",
24+
id: options.searchProviders[i],
25+
title: searchProviderNames[options.searchProviders[i]],
26+
contexts: ["image"]
27+
});
28+
}
29+
}
830

931
/* Default settings. If there is nothing in storage, use these values. */
1032
var defaultSettings = {
1133
openInBackground: false,
1234
openTabAt: "right",
13-
searchProvider: "google"
14-
}
15-
16-
/* Generic error logger. */
17-
function onError(e) {
18-
console.error(e);
35+
searchProviders: ["google"]
1936
}
2037

21-
/* On startup, check whether we have stored settings.
22-
If we don't, then store the default settings. */
38+
/* On startup, check whether we have stored settings and set up the context menu.
39+
If we don't, then store the default settings. */
2340
function checkStoredSettings(storedSettings) {
24-
if (!storedSettings.openInBackground || !storedSettings.openTabAt || !storedSettings.searchProvider) {
41+
if (storedSettings.openInBackground == null || !storedSettings.openTabAt || !storedSettings.searchProviders) {
2542
chrome.storage.sync.set(defaultSettings);
43+
createContextMenu(defaultSettings);
44+
} else {
45+
chrome.storage.sync.get("searchProviders", createContextMenu);
2646
}
2747
}
2848

29-
const gettingStoredSettings = chrome.storage.sync.get(null, checkStoredSettings);
30-
3149
function reverseSearch(info, storedSettings) {
3250
function getTabIndex(openTabAt, tabs) {
3351
if (openTabAt == 'right') {
@@ -39,32 +57,32 @@ function reverseSearch(info, storedSettings) {
3957
}
4058
}
4159

42-
function getSearchProvider(searchProvider) {
43-
if (searchProvider == 'google') {
60+
function getSearchProviderURL(searchProviderName) {
61+
if (searchProviderName == 'google') {
4462
return 'https://www.google.com/searchbyimage?image_url=%s';
45-
} else if (searchProvider == 'bing') {
63+
} else if (searchProviderName == 'bing') {
4664
return 'https://www.bing.com/images/searchbyimage?FORM=IRSBIQ&cbir=sbi&imgurl=%s';
47-
} else if (searchProvider == 'yandex') {
65+
} else if (searchProviderName == 'yandex') {
4866
return 'https://yandex.com/images/search?url=%s&rpt=imageview';
49-
} else if (searchProvider == 'yandexru') {
67+
} else if (searchProviderName == 'yandexru') {
5068
return 'https://yandex.ru/images/search?url=%s&rpt=imageview';
51-
} else if (searchProvider == 'tineye') {
69+
} else if (searchProviderName == 'tineye') {
5270
return 'https://www.tineye.com/parse?url=%s';
53-
} else if (searchProvider == 'baidu') {
71+
} else if (searchProviderName == 'baidu') {
5472
return 'https://image.baidu.com/n/pc_search?queryImageUrl=%s'
55-
} else if (searchProvider == 'saucenao') {
73+
} else if (searchProviderName == 'saucenao') {
5674
return 'https://saucenao.com/search.php?db=999&url=%s'
57-
} else if (searchProvider == 'iqdb') {
75+
} else if (searchProviderName == 'iqdb') {
5876
return 'https://iqdb.org/?url=%s'
59-
} else if (searchProvider == 'other') {
77+
} else if (searchProviderName == 'other') {
6078
return storedSettings.cseProvider;
6179
}
6280
}
6381

6482
const imageURL = info.srcUrl;
6583
const openInBackground = storedSettings.openInBackground;
6684
const openTabAt = storedSettings.openTabAt;
67-
const searchProvider = getSearchProvider(storedSettings.searchProvider);
85+
const searchProvider = getSearchProviderURL(info.menuItemId);
6886

6987
function openImageSearch(tabs) {
7088
tabIndex = getTabIndex(openTabAt, tabs);
@@ -78,6 +96,20 @@ function reverseSearch(info, storedSettings) {
7896
chrome.tabs.query({currentWindow: true, active: true}, openImageSearch);
7997
}
8098

99+
/* Load search engine names and settings and set up context menu */
100+
const searchProviderNames = {
101+
google: "Google",
102+
bing: "Bing",
103+
yandex: "Yandex",
104+
yandexru: "Яндекс",
105+
baidu: "Baidu",
106+
tineye: "TinEye",
107+
saucenao: "SauceNAO",
108+
iqdb: "IQDB",
109+
other: chrome.i18n.getMessage("customSearchProviderLabel")
110+
};
111+
chrome.storage.sync.get(null, checkStoredSettings);
112+
81113
/* On click, fetch stored settings and reverse search. */
82114
chrome.contextMenus.onClicked.addListener((info, tab) => {
83115
chrome.storage.sync.get(null, reverseSearch.bind(null, info));

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"name": "__MSG_extensionName__",
55
"description": "__MSG_extensionDescription__",
66
"author": "Andreas Bielawski",
7-
"version": "2.1.2",
7+
"version": "3.0.0",
88
"icons" : {
99
"16": "icons/16.png",
1010
"32": "icons/32.png",

options/options.html

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
.custom-control-indicator {
1717
background-color: #fff;
1818
}
19+
20+
.jumbotron {
21+
padding: 2rem 2rem;
22+
}
1923
</style>
2024
</head>
2125
<body>
@@ -31,25 +35,69 @@
3135
<div class="container">
3236
<div class="jumbotron">
3337

34-
<!-- "Search provider" select dropdown -->
38+
<!-- Search provider list -->
3539
<div class="form-group row">
3640
<label for="searchProvider" class="col-sm-3 col-form-label" id="searchProviderLabel"></label>
3741
<div class="col-sm-9">
38-
<select id="searchProvider" class="custom-select">
39-
<option value="google" selected>Google</option>
40-
<option value="bing">Bing</option>
41-
<option value="yandex">Yandex</option>
42-
<option value="yandexru">Яндекс</option>
43-
<option value="baidu">Baidu</option>
44-
<option value="tineye">TinEye</option>
45-
<option value="saucenao">SauceNAO</option>
46-
<option value="iqdb">IQDB</option>
47-
<option id="otherCSE" value="other"></option>
48-
</select>
42+
<div class="form-check form-check-inline">
43+
<label class="form-check-label custom-control custom-checkbox">
44+
<input class="form-check-input custom-control-input" type="checkbox" id="google" value="google">
45+
<span class="custom-control-indicator"></span> Google
46+
</label>
47+
</div><br>
48+
<div class="form-check form-check-inline">
49+
<label class="form-check-label custom-control custom-checkbox">
50+
<input class="form-check-input custom-control-input" type="checkbox" id="bing" value="bing">
51+
<span class="custom-control-indicator"></span> Bing
52+
</label>
53+
</div><br>
54+
<div class="form-check form-check-inline">
55+
<label class="form-check-label custom-control custom-checkbox">
56+
<input class="form-check-input custom-control-input" type="checkbox" id="yandex" value="yandex">
57+
<span class="custom-control-indicator"></span> Yandex
58+
</label>
59+
</div><br>
60+
<div class="form-check form-check-inline">
61+
<label class="form-check-label custom-control custom-checkbox">
62+
<input class="form-check-input custom-control-input" type="checkbox" id="yandexru" value="yandexru">
63+
<span class="custom-control-indicator"></span> Яндекс
64+
</label>
65+
</div><br>
66+
<div class="form-check form-check-inline">
67+
<label class="form-check-label custom-control custom-checkbox">
68+
<input class="form-check-input custom-control-input" type="checkbox" id="baidu" value="baidu">
69+
<span class="custom-control-indicator"></span> Baidu
70+
</label>
71+
</div><br>
72+
<div class="form-check form-check-inline">
73+
<label class="form-check-label custom-control custom-checkbox">
74+
<input class="form-check-input custom-control-input" type="checkbox" id="tineye" value="tineye">
75+
<span class="custom-control-indicator"></span> TinEye
76+
</label>
77+
</div><br>
78+
<div class="form-check form-check-inline">
79+
<label class="form-check-label custom-control custom-checkbox">
80+
<input class="form-check-input custom-control-input" type="checkbox" id="saucenao" value="saucenao">
81+
<span class="custom-control-indicator"></span> SauceNAO
82+
</label>
83+
</div><br>
84+
<div class="form-check form-check-inline">
85+
<label class="form-check-label custom-control custom-checkbox">
86+
<input class="form-check-input custom-control-input" type="checkbox" id="iqdb" value="iqdb">
87+
<span class="custom-control-indicator"></span> IQDB
88+
</label>
89+
</div><br>
90+
<div class="form-check form-check-inline">
91+
<label class="form-check-label custom-control custom-checkbox">
92+
<input class="form-check-input custom-control-input" type="checkbox" id="other" value="other">
93+
<span class="custom-control-indicator"></span> <span id="otherCSE"></span>
94+
</label>
95+
</div>
4996
</div>
97+
<div class="alert alert-danger col-sm-12 hidden" role="alert" id="searchProvidersError"></div>
5098
</div>
5199

52-
<!-- Custom search provider -->
100+
<!-- Custom search provider -->
53101
<div id="customSearchProviderForm" class="form-group row hidden">
54102
<label for="customSearchProvider" class="col-sm-3 col-form-label" id="customSearchProviderLabel"></label>
55103
<div class="col-sm-9">
@@ -85,7 +133,7 @@
85133

86134
<!-- "Save options" button -->
87135
<button type="button" id="save-button" class="btn btn-success"></button><br><br>
88-
<div class="alert alert-success" role="alert" id="status"></div>
136+
<div class="alert" role="alert" id="status"></div>
89137

90138
</div> <!-- ./jumbotron -->
91139
</div> <!-- ./container -->

0 commit comments

Comments
 (0)