-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
65 lines (59 loc) · 2.71 KB
/
background.js
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
var browser = browser || chrome;
let user_agent = "";
let browserStorage = browser.storage.sync.get("user_agent");
browserStorage.then((item) => {if (item.user_agent) { user_agent = item.user_agent; } else { user_agent = "Mozilla/5.0 (Windows Phone 10.0; Android 6.0.1; Xbox; Xbox One) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Mobile Safari/537.36 Mobile Edge/42.0.0.2028"; }});
let remove_artifacts = false;
let browserStorage2 = browser.storage.sync.get("remove_artifacts");
browserStorage2.then((item) => {remove_artifacts = item.remove_artifacts;});
let isAndroid = false;
let platformInfo = browser.runtime.getPlatformInfo();
platformInfo.then((value) => {
if (value.os === "android") {
isAndroid = true;
}
});
browser.webRequest.onBeforeSendHeaders.addListener(
function (details) {
console.log(details);
if (!details.requestHeaders) {
return;
}
if (details.url.startsWith("https://studio.youtube.com") || details.url.startsWith("http://studio.youtube.com")) { // fix for YouTube Studio "You are using an unsupported browser" message
return;
}
if (details.url.startsWith("https://accounts.youtube.com") || details.url.startsWith("http://accounts.youtube.com")) { // fix for Google Docs/Drive becoming "unsupported"
return;
}
for (let i = 0; i < details.requestHeaders.length; i++) {
if (details.requestHeaders[i].name.toLowerCase() === "user-agent") { // headers are case-insensitive
if (remove_artifacts === true) {
console.log(`${details.url}, ${details.originUrl}}`);
if (details.originUrl) { // if the requested URL is not from an outside source (ex: the webpage itself), then set the user agent
details.requestHeaders[i].value = user_agent;
}
}
else {
details.requestHeaders[i].value = user_agent;
}
break;
}
}
return { requestHeaders: details.requestHeaders };
},
{ urls: ["*://*.youtube.com/*"] },
["blocking", "requestHeaders"]
);
browser.webRequest.onBeforeRequest.addListener(
function(details) {
if (isAndroid) { return {}; }
if (details.url.startsWith("https://m.youtube.com") || details.url.startsWith("http://m.youtube.com")) {
const urlString = details.url.replace("m.youtube.com", "www.youtube.com");
const url = new URL(urlString);
url.searchParams.set('app', 'desktop');
return { redirectUrl: url.href };
}
return {};
},
{ urls: ["*://m.youtube.com/*"] },
["blocking"]
);