|
1 | 1 | import { useEffect } from 'react';
|
| 2 | +import { browser } from 'webextension-polyfill-ts'; |
2 | 3 |
|
3 | 4 | /**
|
4 | 5 | * Adds an event listener to the window.
|
@@ -29,83 +30,31 @@ export class WindowProxy {
|
29 | 30 | * @param property Property to retrieve from proxy window.
|
30 | 31 | */
|
31 | 32 | getProperty<T = any>(property: string): Promise<T> {
|
32 |
| - /** |
33 |
| - * Adds the proxy window script to the page. |
34 |
| - */ |
35 |
| - const addProxyScript = (): HTMLScriptElement => { |
| 33 | + return new Promise((resolve, reject) => { |
36 | 34 | const script = document.createElement('script');
|
37 |
| - const scriptContent = document.createTextNode(` |
38 |
| - const { currentScript } = document; |
39 |
| -
|
40 |
| - /** |
41 |
| - * Retrieves the property and sets it as the data attribute. |
42 |
| - */ |
43 |
| - const setAttribute = () => { |
44 |
| - const { property } = currentScript.dataset; |
45 |
| -
|
46 |
| - currentScript.dataset.value = JSON.stringify(window[property]); |
47 |
| - } |
48 |
| -
|
49 |
| - /** |
50 |
| - * Main function. |
51 |
| - */ |
52 |
| - (() => { |
53 |
| - if (document.readyState === 'complete') { |
54 |
| - setAttribute(); |
55 |
| -
|
56 |
| - return; |
57 |
| - } |
58 |
| -
|
59 |
| - const listener = () => { |
60 |
| - setAttribute(); |
61 |
| -
|
62 |
| - window.removeEventListener('load', listener); |
63 |
| - }; |
64 |
| -
|
65 |
| - window.addEventListener('load', listener); |
66 |
| - })(); |
67 |
| - `); |
68 | 35 |
|
| 36 | + script.src = browser.runtime.getURL('window-proxy-script.js'); |
69 | 37 | script.dataset.property = property;
|
70 |
| - script.appendChild(scriptContent); |
71 | 38 |
|
72 |
| - document.head.appendChild(script); |
| 39 | + (document.head || document.documentElement).appendChild(script); |
73 | 40 |
|
74 |
| - return script; |
75 |
| - }; |
| 41 | + new MutationObserver((_mutations, observer) => { |
| 42 | + const { value } = script.dataset; |
76 | 43 |
|
77 |
| - return new Promise((resolve, reject) => { |
78 |
| - /** |
79 |
| - * Retrieves the data from the script tag. |
80 |
| - */ |
81 |
| - const getData = (): void => { |
82 |
| - const script = addProxyScript(); |
83 |
| - |
84 |
| - try { |
85 |
| - const { value } = script.dataset; |
86 |
| - // script.remove(); |
87 |
| - |
88 |
| - if (!value) { |
89 |
| - reject(new Error('The value is empty')); |
| 44 | + if (!value) { |
| 45 | + return; |
| 46 | + } |
90 | 47 |
|
91 |
| - return; |
92 |
| - } |
| 48 | + observer.disconnect(); |
93 | 49 |
|
| 50 | + try { |
94 | 51 | resolve(JSON.parse(value));
|
95 | 52 | } catch (error: any) {
|
96 | 53 | reject(error);
|
97 | 54 | }
|
98 |
| - }; |
99 |
| - |
100 |
| - if (document.readyState === 'complete') { |
101 |
| - getData(); |
102 |
| - |
103 |
| - return; |
104 |
| - } |
105 | 55 |
|
106 |
| - window.addEventListener('load', () => { |
107 |
| - getData(); |
108 |
| - }); |
| 56 | + script.remove(); |
| 57 | + }).observe(script, { attributes: true }); |
109 | 58 | });
|
110 | 59 | }
|
111 | 60 | }
|
0 commit comments