2023-04-17 02:05:01 +00:00
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {T[]} arr
|
|
|
|
* @param {(v: T) => boolean} predicate
|
|
|
|
*/
|
|
|
|
function removeFirst(arr, predicate) {
|
|
|
|
const idx = arr.findIndex(predicate);
|
|
|
|
if (idx !== -1) arr.splice(idx, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.webRequest.onHeadersReceived.addListener(
|
|
|
|
({ responseHeaders, type, url }) => {
|
|
|
|
if (!responseHeaders) return;
|
|
|
|
|
|
|
|
if (type === "main_frame") {
|
|
|
|
// In main frame requests, the CSP needs to be removed to enable fetching of custom css
|
|
|
|
// as desired by the user
|
|
|
|
removeFirst(responseHeaders, h => h.name.toLowerCase() === "content-security-policy");
|
2023-04-29 02:16:28 +00:00
|
|
|
} else if (type === "stylesheet" && url.startsWith("https://raw.githubusercontent.com/")) {
|
2023-04-17 02:05:01 +00:00
|
|
|
// Most users will load css from GitHub, but GitHub doesn't set the correct content type,
|
|
|
|
// so we fix it here
|
|
|
|
removeFirst(responseHeaders, h => h.name.toLowerCase() === "content-type");
|
|
|
|
responseHeaders.push({
|
|
|
|
name: "Content-Type",
|
|
|
|
value: "text/css"
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return { responseHeaders };
|
|
|
|
},
|
|
|
|
{ urls: ["https://raw.githubusercontent.com/*", "*://*.discord.com/*"], types: ["main_frame", "stylesheet"] },
|
|
|
|
["blocking", "responseHeaders"]
|
|
|
|
);
|