Skip to content

Commit 9b03230

Browse files
authored
fix: global access to properties
1 parent 7d3dce1 commit 9b03230

1 file changed

Lines changed: 37 additions & 18 deletions

File tree

index.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,45 @@
1-
// Polyfill window
2-
global.window = {};
3-
global.document = {};
1+
// Polyfills
42

5-
window.localStorage = {
3+
global.document = {
4+
createElement: () => ({
5+
addEventListener: () => {},
6+
style: {},
7+
classList: { add: () => {}, remove: () => {} },
8+
setAttribute: () => {},
9+
remove: () => {},
10+
}),
11+
body: {
12+
appendChild: () => {},
13+
},
14+
};
15+
16+
global.localStorage = {
617
store: {},
7-
getItem: (key = '') => window.localStorage.store[key],
18+
getItem: (key = '') => localStorage.store[key],
819
setItem: (key = '', value = '') => {
9-
window.localStorage.store[key] = value;
20+
localStorage.store[key] = value;
1021
},
1122
removeItem: (key = '') => {
12-
delete window.localStorage.store[key];
23+
delete localStorage.store[key];
1324
}
1425
};
1526

16-
window.sessionStorage = {
27+
global.sessionStorage = {
1728
store: {},
18-
getItem: (key = '') => window.sessionStorage.store[key],
29+
getItem: (key = '') => sessionStorage.store[key],
1930
setItem: (key = '', value = '') => {
20-
window.sessionStorage.store[key] = value;
31+
sessionStorage.store[key] = value;
2132
},
2233
removeItem: (key = '') => {
23-
delete window.sessionStorage.store[key];
34+
delete sessionStorage.store[key];
2435
}
2536
};
2637

27-
window.navigator = {
38+
global.navigator = {
2839
language: 'en',
2940
};
3041

31-
window.document = {
42+
global.document = {
3243
createElement: () => ({
3344
addEventListener: () => {},
3445
style: {},
@@ -41,7 +52,7 @@ window.document = {
4152
},
4253
};
4354

44-
window.location = {
55+
global.location = {
4556
ancestorOrigins: {},
4657
assign: () => {},
4758
reload: () => {},
@@ -58,10 +69,18 @@ window.location = {
5869
search: '',
5970
};
6071

61-
window.top = {
62-
location: window.location,
72+
global.top = {
73+
location,
6374
};
6475

65-
window.parent = {
66-
location: window.location,
76+
global.parent = {
77+
location,
6778
}
79+
80+
global.window = {
81+
localStorage,
82+
sessionStorage,
83+
navigator,
84+
document,
85+
localtion,
86+
};

0 commit comments

Comments
 (0)