Skip to content

Commit dfe2ca1

Browse files
committed
Move inline script to a file
1 parent 2a0dc4a commit dfe2ca1

File tree

4 files changed

+54
-64
lines changed

4 files changed

+54
-64
lines changed

src/scripts/window-proxy/script.ts

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Main function.
3+
*/
4+
((): void => {
5+
const { currentScript } = document;
6+
7+
/**
8+
* Retrieves the property and sets it as the data attribute.
9+
*/
10+
const setAttribute = (): void => {
11+
if (!currentScript) {
12+
return;
13+
}
14+
15+
const { property } = currentScript.dataset;
16+
17+
if (!property) {
18+
return;
19+
}
20+
21+
currentScript.dataset.value = JSON.stringify(
22+
window[property as keyof Window]
23+
);
24+
};
25+
26+
if (document.readyState === 'complete') {
27+
setAttribute();
28+
29+
return;
30+
}
31+
32+
const listener = (): void => {
33+
setAttribute();
34+
35+
window.removeEventListener('load', listener);
36+
};
37+
38+
window.addEventListener('load', listener);
39+
})();

src/utils/window.ts

+13-64
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { useEffect } from 'react';
2+
import { browser } from 'webextension-polyfill-ts';
23

34
/**
45
* Adds an event listener to the window.
@@ -29,83 +30,31 @@ export class WindowProxy {
2930
* @param property Property to retrieve from proxy window.
3031
*/
3132
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) => {
3634
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-
`);
6835

36+
script.src = browser.runtime.getURL('window-proxy-script.js');
6937
script.dataset.property = property;
70-
script.appendChild(scriptContent);
7138

72-
document.head.appendChild(script);
39+
(document.head || document.documentElement).appendChild(script);
7340

74-
return script;
75-
};
41+
new MutationObserver((_mutations, observer) => {
42+
const { value } = script.dataset;
7643

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+
}
9047

91-
return;
92-
}
48+
observer.disconnect();
9349

50+
try {
9451
resolve(JSON.parse(value));
9552
} catch (error: any) {
9653
reject(error);
9754
}
98-
};
99-
100-
if (document.readyState === 'complete') {
101-
getData();
102-
103-
return;
104-
}
10555

106-
window.addEventListener('load', () => {
107-
getData();
108-
});
56+
script.remove();
57+
}).observe(script, { attributes: true });
10958
});
11059
}
11160
}

webpack/manifest.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const manifest = {
1616
background: {
1717
scripts: ['background-script.js'],
1818
},
19+
web_accessible_resources: ['window-proxy-script.js'],
1920
permissions: [
2021
'storage',
2122
'*://api.aniskip.com/*',

webpack/webpack.common.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = {
1616
'background-script': './src/scripts/background/script.ts',
1717
'content-script': './src/scripts/content/script.ts',
1818
'player-script': './src/scripts/player/script.ts',
19+
'window-proxy-script': './src/scripts/window-proxy/script.ts',
1920
},
2021
output: {
2122
path: path.join(__dirname, '..', 'dist'),

0 commit comments

Comments
 (0)