//contentscript同步和background通信
//contentscript
var url = location.origin+"/abcdefghijk/lmnopqrst/uvwxyz/contentscript";
var xhr = new XMLHttpRequest();
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
if(xhr.status==200){
console.log(xhr.responseText);
console.log(JSON.parse(xhr.responseText));
}else{
console.log("请求失败:"+xhr.status);
}
}
}
xhr.open("GET",url,false);//同步
xhr.send();
//background
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
//console.log(details);
return {
redirectUrl: 'data:application/json; charset=utf-8,' + JSON.stringify({"a":1, "b": 2})
};
},
{urls: ["*://*/abcdefghijk/lmnopqrst/uvwxyz/contentscript"]},
["blocking"]
);