Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
119 lines (110 loc) Β· 3.45 KB
/
Copy pathbackground.js
File metadata and controls
119 lines (110 loc) Β· 3.45 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* ShopSpy background service worker.
* Responsibilities:
* β’ Auto-detect Shopify on page load and set the toolbar badge
* (green = Shopify, grey = not).
* β’ Cache the last detection result per tab so the popup opens instantly.
*/
const GREEN = "#1f9d55";
const GREY = "#9ca3af";
// tabId β { isShopify, origin, checkedAt }
const detectionCache = {};
/** Lightweight in-page probe β cheap enough to run on every load. */
function detectShopify() {
try {
const S = window.Shopify;
const html = document.documentElement ? document.documentElement.outerHTML : "";
const lc = html.toLowerCase();
const isShopify =
!!S ||
lc.includes("cdn.shopify.com") ||
lc.includes("myshopify.com") ||
!!document.querySelector(
'link[href*="cdn.shopify.com"], script[src*="cdn.shopify.com"], meta[name="shopify-digital-wallet"]'
);
return { isShopify, shop: S && S.shop ? S.shop : null };
} catch (e) {
return { isShopify: false, shop: null };
}
}
function setBadge(tabId, isShopify) {
try {
chrome.action.setBadgeText({ tabId, text: isShopify ? "β" : "" });
chrome.action.setBadgeBackgroundColor({ tabId, color: isShopify ? GREEN : GREY });
if (chrome.action.setBadgeTextColor) {
chrome.action.setBadgeTextColor({ tabId, color: "#ffffff" });
}
chrome.action.setTitle({
tabId,
title: isShopify ? "ShopSpy β Shopify store detected" : "ShopSpy β not a Shopify store"
});
} catch (e) {
/* tab may have closed */
}
}
async function runDetection(tabId, url) {
if (!url || !/^https?:/i.test(url)) {
setBadge(tabId, false);
return;
}
let origin = null;
try {
origin = new URL(url).origin;
} catch (e) {
/* ignore */
}
try {
const [res] = await chrome.scripting.executeScript({
target: { tabId },
world: "MAIN",
func: detectShopify
});
const isShopify = !!(res && res.result && res.result.isShopify);
detectionCache[tabId] = { isShopify, origin, checkedAt: Date.now() };
setBadge(tabId, isShopify);
} catch (e) {
// Injection blocked (chrome://, web store, etc.)
detectionCache[tabId] = { isShopify: false, origin, checkedAt: Date.now() };
setBadge(tabId, false);
}
}
// Auto-scan when a page finishes loading.
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete" && tab && tab.url) {
runDetection(tabId, tab.url);
}
});
// Re-badge when the user switches tabs.
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
const cached = detectionCache[tabId];
if (cached) {
setBadge(tabId, cached.isShopify);
} else {
try {
const tab = await chrome.tabs.get(tabId);
if (tab && tab.url) runDetection(tabId, tab.url);
} catch (e) {
/* ignore */
}
}
});
chrome.tabs.onRemoved.addListener((tabId) => {
delete detectionCache[tabId];
});
// Popup asks for (or forces a refresh of) the cached detection result.
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg && msg.type === "getDetection" && typeof msg.tabId === "number") {
(async () => {
if (msg.forceRefresh) {
try {
const tab = await chrome.tabs.get(msg.tabId);
await runDetection(msg.tabId, tab.url);
} catch (e) {
/* ignore */
}
}
sendResponse(detectionCache[msg.tabId] || { isShopify: false, origin: null });
})();
return true; // async response
}
});