Last active
November 28, 2016 23:37
-
-
Save akirattii/f3d135c0917f9a209d3756b34a2ff1f9 to your computer and use it in GitHub Desktop.
Chrome extention example modify request headers of browser even 'Referer' etc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Headers you want to modify. | |
| let modifyingHeaders = { | |
| "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36", | |
| "Origin": "http://example", | |
| "Referer": "http://example/foo/bar", | |
| "Cookie": 'key1="value1"; key2="value2";', | |
| }; | |
| let requestFilter = { | |
| // urls: ["<all_urls>"] | |
| urls: ["*://example/*"] | |
| }; | |
| let extraInfoSpec = [ | |
| 'requestHeaders', | |
| 'blocking' | |
| ]; | |
| let handler = function(details) { | |
| let headers = details.requestHeaders; | |
| let blockingResponse = {}; | |
| let header, headername, modifyingHeaderVal; | |
| // Modify target headers. | |
| for (let i = 0, len = headers.length; i < len; i++) { | |
| header = headers[i]; | |
| headername = header.name; | |
| modifyingHeaderVal = modifyingHeaders[headername]; | |
| if (modifyingHeaderVal) { | |
| header.value = modifyingHeaderVal | |
| } | |
| } | |
| // If header you want to change did not exist in headers[i], push that as new directly. | |
| let exists; | |
| Object.keys(modifyingHeaders).forEach(function(k) { | |
| exists = headers[k]; | |
| if (!exists) { | |
| headers.push({ name: k, value: modifyingHeaders[k] }); | |
| } | |
| }); | |
| blockingResponse.requestHeaders = headers; | |
| return blockingResponse; | |
| }; | |
| // Execute header changing | |
| chrome.webRequest | |
| .onBeforeSendHeaders | |
| .addListener(handler, requestFilter, extraInfoSpec); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "RequestHeaderChanger", | |
| "version": "0.0.1", | |
| "permissions": [ | |
| "<all_urls>", | |
| "webRequest", | |
| "webRequestBlocking", | |
| "background", | |
| "activeTab" | |
| ], | |
| "background": { | |
| "scripts": ["background.js"] | |
| // "persistent": false | |
| }, | |
| "browser_action": { | |
| "default_title": "popup", | |
| "default_icon": "icon.png", | |
| "default_popup": "popup.html" | |
| }, | |
| "manifest_version": 2, | |
| "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment