Refactor background.js to dynamically adjust User-Agent based on OS; update manifest.json version and description; add .gitignore for build artifacts

This commit is contained in:
암냥 2026-07-27 13:15:58 +09:00
commit 0eea23ef88
No known key found for this signature in database
3 changed files with 72 additions and 13 deletions

View file

@ -1,18 +1,73 @@
"use strict";
const WINDOWS_CHROME_UA =
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
"AppleWebKit/537.36 (KHTML, like Gecko) " +
"Chrome/140.0.0.0 Safari/537.36";
const CHROME_VERSION = "140.0.0.0";
const TARGET_HOSTS = [
"netflix.com",
"assets.nflxext.com"
];
browser.webRequest.onBeforeSendHeaders.addListener(
function getOperatingSystem() {
const ua = globalThis.navigator?.userAgent || "";
const platform = globalThis.navigator?.platform || "";
const userAgentDataPlatform = globalThis.navigator?.userAgentData?.platform || "";
if (/Windows/i.test(ua) || /Win/i.test(platform) || /win/i.test(userAgentDataPlatform)) {
return "win";
}
if (/Mac/i.test(ua) || /Mac/i.test(platform) || /mac/i.test(userAgentDataPlatform)) {
return "mac";
}
if (/Linux/i.test(ua) || /Linux/i.test(platform) || /linux/i.test(userAgentDataPlatform)) {
return "linux";
}
return "linux";
}
function getChromeLikeUserAgent() {
const os = getOperatingSystem();
switch (os) {
case "mac":
return `Mozilla/5.0 (Macintosh; Intel Mac OS X 14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${CHROME_VERSION} Safari/537.36`;
case "win":
return `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${CHROME_VERSION} Safari/537.36`;
case "linux":
default:
return `Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${CHROME_VERSION} Safari/537.36`;
}
}
function isTargetHost(url) {
try {
const hostname = new URL(url).hostname.toLowerCase();
return hostname === "netflix.com" ||
hostname.endsWith(".netflix.com") ||
hostname === "assets.nflxext.com" ||
hostname.endsWith(".nflxext.com");
} catch {
return false;
}
}
const browserApi = globalThis.browser || globalThis.chrome;
browserApi.webRequest.onBeforeSendHeaders.addListener(
(details) => {
if (!isTargetHost(details.url)) {
return {};
}
const requestHeaders = details.requestHeaders ?? [];
const userAgentValue = getChromeLikeUserAgent();
let foundUserAgent = false;
for (const header of requestHeaders) {
if (header.name.toLowerCase() === "user-agent") {
header.value = WINDOWS_CHROME_UA;
header.value = userAgentValue;
foundUserAgent = true;
break;
}
@ -21,7 +76,7 @@ browser.webRequest.onBeforeSendHeaders.addListener(
if (!foundUserAgent) {
requestHeaders.push({
name: "User-Agent",
value: WINDOWS_CHROME_UA
value: userAgentValue
});
}
@ -30,7 +85,9 @@ browser.webRequest.onBeforeSendHeaders.addListener(
{
urls: [
"*://netflix.com/*",
"*://*.netflix.com/*"
"*://*.netflix.com/*",
"*://assets.nflxext.com/*",
"*://*.nflxext.com/*"
]
},
[