diff --git a/package.json b/package.json index bcddb428..005ead5c 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ }, "devDependencies": { "@types/axios": "0.14.0", + "@types/chrome": "^0.0.70", "@types/webpack": "4.4.0", "prettier": "^1.13.4", "ts-loader": "4.3.0", diff --git a/release/manifest.json b/release/manifest.json index 7b28bc06..43745a6d 100644 --- a/release/manifest.json +++ b/release/manifest.json @@ -2,8 +2,10 @@ "name": "Ku-nya", "version": "1.0.1", "manifest_version": 2, - "description": "Ku-nya picks up illustrations on the pixiv ranking randomly and displays them on your new tab. Enjoy your pixiv life!", - "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'", + "description": + "Ku-nya picks up illustrations on the pixiv ranking randomly and displays them on your new tab. Enjoy your pixiv life!", + "content_security_policy": + "script-src 'self' 'unsafe-eval'; object-src 'self'", "icons": {}, "chrome_url_overrides": { "newtab": "newTab.html" @@ -12,5 +14,10 @@ "default_popup": "popup.html" }, "background": {}, - "permissions": ["https://www.pixiv.net/"] + "permissions": [ + "webRequest", + "webRequestBlocking", + "https://www.pixiv.net/", + "https://i.pximg.net/" + ] } diff --git a/src/lib/requestModifier.ts b/src/lib/requestModifier.ts new file mode 100644 index 00000000..89e0a91a --- /dev/null +++ b/src/lib/requestModifier.ts @@ -0,0 +1,29 @@ +const refererValue = 'https://www.pixiv.net/' +const refererTarget = 'https://i.pximg.net/*' + +/** Modifies request Referer HTTP header */ +chrome.webRequest.onBeforeSendHeaders.addListener( + details => { + let refererFound = false + + for (const n in details.requestHeaders) { + refererFound = details.requestHeaders[n].name.toLowerCase() == 'referer' + + if (refererFound) { + // Rewrite Referer header + details.requestHeaders[n].value = refererValue + break + } + } + + if (!refererFound) { + // If no referer header is set, set one + details.requestHeaders.push({ name: 'Referer', value: refererValue }) + } + return { requestHeaders: details.requestHeaders } + }, + { + urls: [refererTarget], + }, + ['requestHeaders', 'blocking'], +) diff --git a/src/main.tsx b/src/main.tsx index a99473fc..67a6ccb7 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,5 +1,6 @@ import { h, render } from 'preact' import App from './components/App' +import './lib/requestModifier' import { getOptions } from './lib/options' document.addEventListener('DOMContentLoaded', async event => { diff --git a/yarn.lock b/yarn.lock index a1966b3f..46881433 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8,6 +8,22 @@ dependencies: axios "*" +"@types/chrome@^0.0.70": + version "0.0.70" + resolved "https://registry.yarnpkg.com/@types/chrome/-/chrome-0.0.70.tgz#634bfa4aff89d00e8c88f6c37b26663e8d7a220e" + dependencies: + "@types/filesystem" "*" + +"@types/filesystem@*": + version "0.0.28" + resolved "https://registry.yarnpkg.com/@types/filesystem/-/filesystem-0.0.28.tgz#3fd7735830f2c7413cb5ac45780bc45904697b0e" + dependencies: + "@types/filewriter" "*" + +"@types/filewriter@*": + version "0.0.28" + resolved "https://registry.yarnpkg.com/@types/filewriter/-/filewriter-0.0.28.tgz#c054e8af4d9dd75db4e63abc76f885168714d4b3" + "@types/node@*": version "8.0.47" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.0.47.tgz#968e596f91acd59069054558a00708c445ca30c2"