Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add support for mangling headers, so people don't have to use other plugins for that #11

Open
wants to merge 1 commit into
base: community
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/background.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
<title>:: Imagus :: bgProcess</title>
<script src="./js/app_bg.js"></script>
<script src="./js/background.js"></script>
</head>
<script src="./js/headers.js"></script>
</head>
93 changes: 93 additions & 0 deletions src/js/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// TODO: Allow configuring, or re-use sieve
let needsTarget = new Set(['webmshare\.com\/', 'i\\d+\.vipr\\.im\/', '\.imagetwist\.com\/']);
let needsRemove = new Set(['sankakucomplex\\.com']);
let needsCustom = new Map([
['tiktokcdn\\.com', 'https://www\\.tiktok\\.com']
]);
let needsBypassCORS = new Set(['\.cdninstagram.com\/']);

chrome.webRequest.onBeforeSendHeaders.addListener(function(details) {
let shouldRemove = false;
let shouldSetTarget = false;
let shouldSetCustom = false;
let newReferer = '';
for (let rule of needsTarget.values()) {
let regex = new RegExp(rule);
if (regex.test(details.url)) {
shouldSetTarget = true;
newReferer = details.url;
break;
}
}
if (!shouldSetTarget) {
for (const rule of needsRemove.values()) {
let regex = new RegExp(rule);
if (regex.test(details.url)) {
shouldRemove = true;
break;
}
}
}

if (!shouldRemove && !shouldSetTarget) {
for (const [rule, value] of needsCustom.entries()) {
let regex = new RegExp(rule);
if (regex.test(details.url)) {
shouldSetCustom = true;
custom = value;
break;
}
}
}

var headers = details.requestHeaders;
if (!shouldRemove && !shouldSetTarget && !shouldSetCustom) {
return {requestHeaders: headers};
}

// Strip out referer
var newHeaders = [];
for(var i = 0, l = headers.length; i < l; ++i) {
if (bypassCORS && headers[i].name.toLowerCase() == 'cross-origin-resource-policy') {
continue;
}
if (headers[i].name.toLowerCase() != 'referer') {
newHeaders.push(headers[i]);
continue;
}
}

if (!shouldRemove) {
newHeaders.push({
name: "Referer",
value: newReferer
});
}

details.requestHeaders = newHeaders
return {requestHeaders: newHeaders};
}, { urls: [ "<all_urls>" ] }, ['requestHeaders','blocking','extraHeaders']);

chrome.webRequest.onHeadersReceived.addListener(function(details) {
let bypassCORS = false;
for (let rule of needsBypassCORS.values()) {
let regex = new RegExp(rule);
if (regex.test(details.url)) {
bypassCORS = true;
break;
}
}
var headers = details.responseHeaders;
if (!bypassCORS) {
return {responseHeaders: headers};
}
var newHeaders = [];
for(var i = 0, l = headers.length; i < l; ++i) {
if (headers[i].name.toLowerCase() == 'cross-origin-resource-policy') {
continue;
}
newHeaders.push(headers[i]);
}
details.responseHeaders = newHeaders
return {responseHeaders: newHeaders};
}, { urls: [ "<all_urls>" ] }, ['responseHeaders', 'blocking', 'extraHeaders']);
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"open_in_tab": true,
"page": "options.html"
},
"permissions": [ "*://*/*", "downloads", "history", "storage" ],
"permissions": [ "*://*/*", "downloads", "history", "storage", "webRequest", "webRequestBlocking" ],
"version": "0.9.8.74"
}