Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
QuiteAFancyEmerald
GitHub Repository: QuiteAFancyEmerald/Holy-Unblocker
Path: blob/master/lib/rammerhead/src/build.js
5256 views
1
require('dotenv-flow').config();
2
3
const path = require('path');
4
const fs = require('fs');
5
const UglifyJS = require('uglify-js');
6
7
// modify unmodifable items that cannot be hooked in rammerhead.js
8
fs.writeFileSync(
9
path.join(__dirname, './client/hammerhead.js'),
10
// part of fix for iframing issue
11
'window["%is-hammerhead%"] = true;\n' +
12
fs
13
.readFileSync(path.join(__dirname, '../node_modules/testcafe-hammerhead/lib/client/hammerhead.js'), 'utf8')
14
// fix iframing proxy issue
15
.replace(
16
/window === window\.top/g,
17
'((window.parent === window.top && !window.top["%hammerhead%"]) || window === window.top)'
18
)
19
.replace(
20
'isCrossDomainParent = parentLocationWrapper === parentWindow.location',
21
'isCrossDomainParent = parentLocationWrapper === parentWindow.location || !parentWindow["%hammerhead%"]'
22
)
23
.replace(
24
'!sameOriginCheck(window1Location, window2Location)',
25
'!(sameOriginCheck(window1Location, window2Location) && (!!window1["%is-hammerhead%"] === !!window2["%is-hammerhead%"]))'
26
)
27
// return false when unable to convert properties on other windows to booleans (!)
28
.replace(
29
/!(parent|parentWindow|window1|window2|window\.top)\[("%(?:is-)?hammerhead%")]/g,
30
'!(() => { try{ return $1[$2]; }catch(error){ return true } })()'
31
)
32
33
// disable saving to localStorage as we are using a completely different implementation
34
.replace('saveToNativeStorage = function () {', 'saveToNativeStorage = function () {return;')
35
36
// prevent calls to elements on a closed iframe
37
.replace('dispatchEvent: function () {', '$& if (!window) return null;')
38
.replace('click: function () {', '$& if (!window) return null;')
39
.replace('setSelectionRange: function () {', '$& if (!window) return null;')
40
.replace('select: function () {', '$& if (!window) return null;')
41
.replace('focus: function () {', '$& if (!window) return null;')
42
.replace('blur: function () {', '$& if (!window) return null;')
43
.replace('preventDefault: function () {', '$& if (!window) return null;')
44
45
// expose hooks for rammerhead.js
46
.replace(
47
'function parseProxyUrl$1',
48
'window.overrideParseProxyUrl = function(rewrite) {parseProxyUrl$$1 = rewrite(parseProxyUrl$$1)}; $&'
49
)
50
.replace(
51
'function getProxyUrl$1',
52
'window.overrideGetProxyUrl = function(rewrite) {getProxyUrl$$1 = rewrite(getProxyUrl$$1)}; $&'
53
)
54
.replace('return window.location.search;', 'return (new URL(get$$2())).search;')
55
.replace('return window.location.hash;', 'return (new URL(get$$2())).hash;')
56
.replace(
57
'setter: function (search) {',
58
'$& var url = new URL(get$$2()); url.search = search; window.location = convertToProxyUrl(url.href); return search;'
59
)
60
.replace(
61
'setter: function (hash) {',
62
'$& var url = new URL(get$$2()); url.hash = hash; window.location.hash = (new URL(convertToProxyUrl(url.href))).hash; return hash;'
63
)
64
);
65
66
const minify = (fileName, newFileName) => {
67
const minified = UglifyJS.minify(fs.readFileSync(path.join(__dirname, './client', fileName), 'utf8'));
68
if (minified.error) {
69
throw minified.error;
70
}
71
fs.writeFileSync(path.join(__dirname, './client', newFileName), minified.code, 'utf8');
72
};
73
74
minify('rammerhead.js', 'rammerhead.min.js');
75
minify('hammerhead.js', 'hammerhead.min.js');
76
77