-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
177 lines (162 loc) · 5.23 KB
/
content.js
File metadata and controls
177 lines (162 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Content script for robust user agent injection
// This script coordinates the injection of scripts into the page context
(function() {
'use strict';
// Inject scripts into page context synchronously at document_start.
// This minimizes timing leaks in aggressive fingerprint checks.
function injectScriptInline(file) {
try {
const xhr = new XMLHttpRequest();
xhr.open('GET', browser.runtime.getURL(file), false); // synchronous
xhr.send();
if (xhr.status === 200 && xhr.responseText) {
const script = document.createElement('script');
script.textContent = xhr.responseText;
const target = document.head || document.documentElement;
if (target) {
target.appendChild(script);
script.remove();
return true;
}
}
} catch (e) {
console.error('[Unga Bunga UA] Failed to inject script:', file, e);
}
return false;
}
// Inject main.js and override.js into page context (MAIN world)
// before page scripts get a chance to run.
injectScriptInline('inject/main.js');
injectScriptInline('inject/override.js');
// Optional experimental noise hooks; loaded only when user enabled it.
browser.storage.local.get('fingerprintNoise').then(stored => {
if (stored && stored.fingerprintNoise === true) {
injectScriptInline('inject/fingerprint-noise.js');
}
}).catch(e => {
console.info('[Unga Bunga UA] fingerprintNoise pref read failed, default off:', e);
});
// Run isolated script code in the content script context (ISOLATED world)
try {
/* global cloneInto */
let port = self.port = document.getElementById('uas-port');
const id = (Math.random() + 1).toString(36).substring(7);
let overrideApplied = false;
const override = reason => {
if (overrideApplied) {
return;
}
overrideApplied = true;
const detail = typeof cloneInto === 'undefined' ? {id, reason} : cloneInto({id, reason}, self);
port.dispatchEvent(new CustomEvent('override', {
detail
}));
};
const requestAsyncOverride = () => {
browser.runtime.sendMessage({
action: 'get-port-string'
}, str => {
if (str && !port.dataset.str) {
port.dataset.str = str;
}
if (port.dataset.str) {
console.info('[Unga Bunga UA] user-agent leaked, using async method:', location.href);
override('async');
}
});
};
if (port) {
// Only touch the port if it looks like the extension-managed element.
const isExtensionPort = port.tagName === 'SPAN' && port.id === 'uas-port';
if (!isExtensionPort) {
console.warn('[Unga Bunga UA] Found non-extension element with id "uas-port"; skipping override path.');
return;
}
port.dataset.id = id;
port.remove();
if (self.top === self) {
if (port.dataset.disabled !== 'true') {
browser.runtime.sendMessage({
action: 'tab-spoofing',
str: port.dataset.str,
type: port.dataset.type
});
}
}
// Start async fallback immediately when server-timing data is missing.
if (!port.dataset.str) {
requestAsyncOverride();
}
}
else { // iframe[sandbox]
try {
const hierarchy = [];
let [p, s] = [parent, self];
for (;;) {
for (let n = 0; n < p.frames.length; n += 1) {
if (p.frames[n] === s) {
hierarchy.unshift(n);
}
}
if (p.port) {
port = p.port;
if (port.dataset.disabled !== 'true') {
port.dispatchEvent(new CustomEvent('register', {
detail: {
id,
hierarchy
}
}));
}
break;
}
[s, p] = [p, p.parent];
if (s === p) {
break;
}
}
}
catch (e) {
console.info('[Unga Bunga UA] user-agent leaked:', e, location.href);
}
}
if (port) {
if (port.dataset.str) {
if (port.dataset.disabled !== 'true') {
override('normal');
}
}
else {
try {
let [p, s] = [parent, self];
for (;;) {
if (p.port) {
if (p.port.dataset.disabled === 'true') {
port.dataset.disabled = true;
}
else {
if ('str' in p.port.dataset) {
port.dataset.str = p.port.dataset.str;
override('parent');
}
}
break;
}
[s, p] = [p, p.parent];
if (s === p) {
break;
}
}
if (!port.dataset.str) {
throw Error('UA_SET_FAILED');
}
}
catch (e) {
requestAsyncOverride();
}
}
}
} catch (e) {
console.error('[Unga Bunga UA] Failed to execute isolated script:', e);
}
})();