|
1 | 1 | import { getOptions } from '../storage'; |
2 | | -import type { CreatePropertiesWithIcon, Options } from '../types'; |
| 2 | +import type { CreatePropertiesWithIcon } from '../types'; |
3 | 3 | import { getMessage, isNullish } from '../utils'; |
4 | 4 |
|
5 | 5 | const PARENT_ID = 'Image-Reverse-Search'; |
@@ -85,46 +85,53 @@ export const onReverseSearch: ( |
85 | 85 | return; |
86 | 86 | } |
87 | 87 |
|
88 | | - const options = await getOptions(); |
| 88 | + const { openTabAt, openInBackground, storageProviders } = await getOptions(); |
89 | 89 |
|
90 | | - const providerUrls = []; |
| 90 | + const activeProviders = storageProviders.filter((provider) => |
| 91 | + menuItemId === OPEN_ALL_ID |
| 92 | + ? provider.selected |
| 93 | + : provider.name === menuItemId, |
| 94 | + ); |
91 | 95 |
|
92 | | - if (menuItemId === 'openAll') { |
93 | | - for (const provider of options.storageProviders) { |
94 | | - if (provider.selected) { |
95 | | - providerUrls.push(provider.url); |
96 | | - } |
| 96 | + if (menuItemId === OPEN_ALL_ID) { |
| 97 | + // Reverse because we open them by tab index |
| 98 | + activeProviders.reverse(); |
| 99 | + } |
| 100 | + |
| 101 | + const newTabIndex = await (async () => { |
| 102 | + switch (openTabAt) { |
| 103 | + case 'right': |
| 104 | + return tab.index + 1; |
| 105 | + case 'left': |
| 106 | + return tab.index; |
| 107 | + default: |
| 108 | + return chrome.tabs |
| 109 | + .query({ currentWindow: true }) |
| 110 | + .then((tabs) => tabs.length) |
| 111 | + .catch(() => tab.index); |
97 | 112 | } |
98 | | - /* Reverse because we open them by tab index */ |
99 | | - providerUrls.reverse(); |
100 | | - } else { |
101 | | - for (const providerId of options.storageProviders) { |
102 | | - if (providerId.name === menuItemId) { |
103 | | - providerUrls.push(providerId.url); |
| 113 | + })(); |
| 114 | + |
| 115 | + await Promise.all( |
| 116 | + activeProviders.map((provider) => { |
| 117 | + let imgSrcUrl = srcUrl; |
| 118 | + if (provider.stripProtocol) { |
| 119 | + imgSrcUrl = imgSrcUrl.replace(/^https?:\/\//, ''); |
104 | 120 | } |
105 | | - } |
106 | | - } |
107 | 121 |
|
108 | | - let newTabIndex: number; |
109 | | - |
110 | | - if (options.openTabAt === 'right') { |
111 | | - newTabIndex = tab.index + 1; |
112 | | - } else if (options.openTabAt === 'left') { |
113 | | - newTabIndex = tab.index; |
114 | | - } else { |
115 | | - /* end */ |
116 | | - newTabIndex = await chrome.tabs |
117 | | - .query({ currentWindow: true }) |
118 | | - .then((tabs) => tabs.length) |
119 | | - .catch(() => tab.index); |
120 | | - } |
| 122 | + let providerUrl = provider.url; |
| 123 | + if (!provider.doNotEncodeUrl) { |
| 124 | + providerUrl = providerUrl.replace('%s', encodeURIComponent(imgSrcUrl)); |
| 125 | + } else { |
| 126 | + providerUrl = providerUrl.replace('%s', imgSrcUrl); |
| 127 | + } |
121 | 128 |
|
122 | | - for (const providerUrl of providerUrls) { |
123 | | - void chrome.tabs.create({ |
124 | | - url: providerUrl.replace('%s', encodeURIComponent(srcUrl)), |
125 | | - active: !options.openInBackground, |
126 | | - index: newTabIndex, |
127 | | - openerTabId: tab.id, |
128 | | - }); |
129 | | - } |
| 129 | + return chrome.tabs.create({ |
| 130 | + url: providerUrl, |
| 131 | + active: !openInBackground, |
| 132 | + index: newTabIndex, |
| 133 | + openerTabId: tab.id, |
| 134 | + }); |
| 135 | + }), |
| 136 | + ); |
130 | 137 | }; |
0 commit comments