Skip to content

Commit

Permalink
Merge pull request #28 from notherno/fix/add_referer_header
Browse files Browse the repository at this point in the history
Send request with a custom Referer header
  • Loading branch information
tamanobi authored Jul 26, 2018
2 parents 409c133 + 20c947e commit 6bfd419
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 10 additions & 3 deletions release/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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/"
]
}
29 changes: 29 additions & 0 deletions src/lib/requestModifier.ts
Original file line number Diff line number Diff line change
@@ -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'],
)
1 change: 1 addition & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -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 => {
Expand Down
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6bfd419

Please # to comment.