Skip to content

Instantly share code, notes, and snippets.

@greatghoul

greatghoul/background.js

Last active Jan 16, 2019
Embed
What would you like to do?
crx-test - Chrome插件开发-请问如何跨域XMLHttpRequest中修改Referer
chrome.webRequest.onBeforeSendHeaders.addListener(
function(details) {
if (details.type === 'xmlhttprequest') {
var exists = false;
for (var i = 0; i < details.requestHeaders.length; ++i) {
if (details.requestHeaders[i].name === 'Referer') {
exists = true;
details.requestHeaders[i].value = 'http://www.baidu.com';
break;
}
}
if (!exists) {
details.requestHeaders.push({ name: 'Referer', value: 'http://www.baidu.com'});
}
return { requestHeaders: details.requestHeaders };
}
},
{urls: ['http://www.geekpark.net/ajax/*']},
["blocking", "requestHeaders"]
);
var url = 'http://www.geekpark.net/ajax/load_seeds/?order=undefined&type=img&tag_id=160388&start=36&num=12&tt=1366382330986&t=';
function test() {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var seeds = JSON.parse(xhr.responseText).data.seeds;
for (var i in seeds) {
console.log(' - ' + seeds[i].title)
}
}
}
}
xhr.send();
}
setInterval(test, 3000);
{
"name": "crx-test",
"version": "1.0",
"manifest_version": 2,
"description": "Chrome插件开发-请问如何跨域XMLHttpRequest中修改Referer",
"background": { "scripts": ["background.js"] },
"permissions": [
"webRequest", "webRequestBlocking", "http://www.geekpark.net/ajax/"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment