|
19 | 19 | 'use strict'; |
20 | 20 |
|
21 | 21 | if (!window.ArmorlyAdPatterns) return; |
22 | | - const sdkFunctions = window.ArmorlyAdPatterns.getAllSDKFunctions(); |
| 22 | + |
| 23 | + // Names we've already installed proxies for. Used to ignore duplicates |
| 24 | + // when the isolated-world script later pushes an updated list pulled |
| 25 | + // from cached_patterns (chrome.storage.local). See message listener below. |
| 26 | + const installed = new Set(); |
23 | 27 |
|
24 | 28 | function createProxy() { |
25 | 29 | return new Proxy({}, { |
|
37 | 41 | }); |
38 | 42 | } |
39 | 43 |
|
40 | | - sdkFunctions.forEach(function (name) { |
41 | | - try { |
42 | | - const proxy = createProxy(); |
43 | | - Object.defineProperty(window, name, { |
44 | | - get: function () { return proxy; }, |
45 | | - set: function () { return true; }, |
46 | | - configurable: false |
47 | | - }); |
48 | | - } catch (_) { |
49 | | - // Property may already be defined non-configurable. Skip. |
| 44 | + function installProxiesFor(names) { |
| 45 | + if (!Array.isArray(names)) return; |
| 46 | + for (const name of names) { |
| 47 | + if (typeof name !== 'string' || installed.has(name)) continue; |
| 48 | + try { |
| 49 | + const proxy = createProxy(); |
| 50 | + Object.defineProperty(window, name, { |
| 51 | + get: function () { return proxy; }, |
| 52 | + set: function () { return true; }, |
| 53 | + configurable: false |
| 54 | + }); |
| 55 | + installed.add(name); |
| 56 | + } catch (_) { |
| 57 | + // Property may already be defined non-configurable on the page; skip. |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // First pass: install from the bundled patterns. Runs synchronously at |
| 63 | + // document_start so the proxy lands before any inline ad-SDK call. |
| 64 | + installProxiesFor(window.ArmorlyAdPatterns.getAllSDKFunctions()); |
| 65 | + |
| 66 | + // Second pass: when ai-ad-blocker.js (isolated world) finishes its |
| 67 | + // chrome.storage.local lookup and merges cached_patterns, it postMessages |
| 68 | + // the updated function list to us so we can proxy any names the daily |
| 69 | + // background refresh added since the bundle was packaged. |
| 70 | + window.addEventListener('message', function (event) { |
| 71 | + if (event.source !== window) return; |
| 72 | + const data = event.data; |
| 73 | + if (!data || data.source !== 'armorly') return; |
| 74 | + if (data.type === 'update-sdk-list' && Array.isArray(data.functions)) { |
| 75 | + installProxiesFor(data.functions); |
50 | 76 | } |
51 | 77 | }); |
52 | 78 | })(); |
0 commit comments