-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
48 lines (40 loc) · 1.3 KB
/
script.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Button handler
*
* @param {string} url
* @param {string} type
*
* @returns {Function<void>}
*/
const handler = (url, type) => {
return () => {
const newPage = {
json: `${url}.json`,
metafields: `${url}/metafields.json`,
}[type];
// We have permission to access the activeTab, so we can call chrome.tabs.executeScript:
chrome.tabs.executeScript({
code: `window.open("${newPage}");` //argument here is a string but function.toString() returns function's code
});
}
};
chrome.tabs.query({ active: true, lastFocusedWindow: true }, (tabs) => {
const currentUrl = tabs[0].url || null;
if (!currentUrl) {
return;
}
const message = document.getElementById('message');
const valid = document.getElementById('valid');
if (currentUrl.match(/myshopify\.com\/admin\/products\/\d+/) !== null) {
message.style.display = 'none';
valid.style.display = 'block';
}
const json = document.getElementById('json-button');
const metafields = document.getElementById('metafields-button');
if (json) {
json.addEventListener('click', handler(currentUrl, 'json'));
}
if (metafields) {
metafields.addEventListener('click', handler(currentUrl, 'metafields'));
}
});