-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
232 lines (201 loc) · 8.36 KB
/
Copy pathbackground.js
File metadata and controls
232 lines (201 loc) · 8.36 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Variable to control whether the update popup should open. "1" to disable.
let disableUpdatePopup = "0";
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'update' && disableUpdatePopup === "0") {
// Open a new tab in a new group when the extension is updated
chrome.tabs.create({ url: 'https://aniziolek.notion.site/Awin-Helper-Updates-1ec7c46530f34c3691e307f498284fd4?pvs=74', active: true }, (tab) => {
chrome.tabs.group({ tabIds: [tab.id] }, (groupId) => {
chrome.tabGroups.update(groupId, {
title: 'AWH Update',
color: 'orange'
});
});
});
}
});
// Send the version from manifest.json to the popup script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.getVersion) {
const manifestData = chrome.runtime.getManifest();
sendResponse({ version: manifestData.version });
}
});
// Replace MIDs with clickable links on specific SF pages
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url.includes("https://awin.lightning.force.com/lightning/r/TSE__c")) {
chrome.scripting.executeScript({
target: { tabId: tabId },
func: transformNumbersToLinks
});
}
});
// Function to replace numbers with clickable links
function transformNumbersToLinks() {
const replaceNumbers = (rootElement = document) => {
rootElement.querySelectorAll('lightning-formatted-text').forEach(element => {
// fp: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
const textContent = element.textContent.trim();
if (/^\d+$/.test(textContent)) { // Check if the content is purely numbers
const numberLink = document.createElement('a');
numberLink.href = `https://ui.awin.com/dashboard/awin/advertiser/${textContent}`;
numberLink.textContent = textContent;
numberLink.style.color = '#fc8e45';
numberLink.style.textDecoration = 'underline';
numberLink.target = '_blank'; // Open in a new tab
numberLink.title = 'Jump to the UI.'; // Tooltip text
element.replaceWith(numberLink);
}
});
};
// Run once when the script is first injected
replaceNumbers();
// Set up a MutationObserver to monitor for changes in the DOM
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE) {
replaceNumbers(node); // Re-run the replacement function only for added nodes
}
});
});
});
// Start observing the document for changes
observer.observe(document.body, {
childList: true,
subtree: true
});
}
// Start of New Implementation Tool
// Create tabs and group them; use the MID passed in from the popup for the group title.
function createImplTabs(urls, tabMID) {
const tabIds = [];
urls.forEach((url) => {
chrome.tabs.create({ url, active: false }, (tab) => {
if (!tab || typeof tab.id !== 'number') return;
tabIds.push(tab.id);
// When all tabs are open, group and label them.
if (tabIds.length === urls.length) {
chrome.tabs.group({ tabIds }, (groupId) => {
if (typeof groupId !== 'number') return;
chrome.tabGroups.update(groupId, { title: tabMID, color: 'orange' });
});
}
});
});
}
// Listen for sanitized MID from the popup and invoke the tab creation.
chrome.runtime.onMessage.addListener((request) => {
if (request?.action !== 'createNITabs') return;
const MIDValue = String(request.mid); // already sanitized in popup
const URLs = [
`https://ui.awin.com/tracking-settings/us/awin/advertiser/${MIDValue}/main-settings`,
`https://ui.awin.com/awin/merchant/${MIDValue}/settings/invite-user`,
`https://ui.awin.com/commission-manager/us/awin/merchant/${MIDValue}/commission-groups`,
`https://ui.awin.com/advertiser-mastertag/us/awin/${MIDValue}/plugins`,
`https://ui.awin.com/advertiser-mastertag/us/awin/${MIDValue}/trackingtagsettings`,
`https://ui.awin.com/provider/merchant-settings/${MIDValue}/account-details/network/awin`,
`https://ui.awin.com/provider/finance/fee-manager/en/${MIDValue}`,
`https://ui.awin.com/provider/merchant-settings/${MIDValue}/mobile-tracking/network/awin`,
`https://ui.awin.com/provider/migrated-advertiser-settings/${MIDValue}`,
`https://ui.awin.com/provider/pre-join-publishers?advertiserId=${MIDValue}`
];
createImplTabs(URLs, MIDValue);
});
// Start Internal Review Tool
function createIRGroupTabs(urls, mid) {
const tabIds = [];
urls.forEach((url) => {
chrome.tabs.create({ url, active: false }, (tab) => {
if (!tab || typeof tab.id !== 'number') return;
tabIds.push(tab.id);
if (tabIds.length === urls.length) {
chrome.tabs.group({ tabIds }, (groupId) => {
if (typeof groupId !== 'number') return;
chrome.tabGroups.update(groupId, { title: `${mid} IR`, color: 'orange' });
});
}
});
});
}
chrome.runtime.onMessage.addListener((request) => {
if (request?.action !== 'createIRTabs') return;
const MIDValue = String(request.mid); // already sanitized in popup
const URLs = [
`https://ui.awin.com/tracking-settings/us/awin/advertiser/${MIDValue}/main-settings`,
`https://ui.awin.com/commission-manager/us/awin/merchant/${MIDValue}/commission-groups`,
`https://ui.awin.com/advertiser-mastertag/us/awin/${MIDValue}/plugins`,
`https://ui.awin.com/awin/merchant/${MIDValue}/validate-pending/network/awin`,
`https://ui.awin.com/advertiser-integration-tool/trackingwizard/us/awin/merchant/${MIDValue}`,
`https://ui.awin.com/provider/test-transactions`,
`https://ui.awin.com/provider/merchant-settings/${MIDValue}/account-details/network/awin`,
`https://ui.awin.com/provider/merchant-settings/${MIDValue}/mobile-tracking/network/awin`,
`https://ui.awin.com/provider/finance/fee-manager/en/${MIDValue}`,
`https://ui.awin.com/provider/pre-join-publishers?advertiserId=${MIDValue}`,
`https://ui.awin.com/provider/migrated-advertiser-settings/${MIDValue}`
];
createIRGroupTabs(URLs, MIDValue);
});
// Tech Detection Script Listeners and Badge
const mem = new Map();
async function setDetections(tabId, items) {
mem.set(tabId, items);
await chrome.storage.session.set({ ['det_'+tabId]: { items, ts: Date.now() } });
}
async function getDetections(tabId) {
if (mem.has(tabId)) return mem.get(tabId);
const obj = await chrome.storage.session.get('det_'+tabId);
return obj?.['det_'+tabId]?.items || [];
}
function setBadge(tabId, count) {
chrome.action.setBadgeText({ tabId, text: count ? String(count) : '' });
chrome.action.setBadgeBackgroundColor({ color: '#FF9550' });
}
// Content script reports
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg?.type === 'TECH_DETECTIONS') {
const tabId = sender?.tab?.id;
if (Number.isInteger(tabId)) {
setDetections(tabId, msg.items || []).then(() => setBadge(tabId, msg.items?.length || 0));
}
}
if (msg?.type === 'GET_TECH_DETECTIONS') {
const tabId = msg.tabId;
(async () => {
const items = await getDetections(tabId);
if (!items.length) {
// ask the content script in that tab to re-run immediately
try { chrome.tabs.sendMessage(tabId, { type: 'REDETECT_NOW' }); } catch {}
}
sendResponse({ items });
})();
return true; // async
}
});
// Keep badge coherent when focus changes
chrome.tabs.onActivated.addListener(async ({ tabId }) => {
const items = await getDetections(tabId);
setBadge(tabId, items.length);
});
chrome.windows.onFocusChanged.addListener(async () => {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab?.id) {
const items = await getDetections(tab.id);
setBadge(tab.id, items.length);
}
});
// Clear state when tabs truly go away or reload
chrome.tabs.onRemoved.addListener((tabId) => {
mem.delete(tabId);
chrome.storage.session.remove('det_'+tabId);
});
chrome.tabs.onUpdated.addListener((tabId, info) => {
if (info.discarded === true) { // tab got discarded
// keep storage; badge can reflect last known
return;
}
if (info.status === 'loading') {
mem.delete(tabId);
setBadge(tabId, 0);
// content script will re-emit on DOMContentLoaded; if it doesn’t, popup will ping via GET handler
}
});