问题
I'm trying to use webRequest API to change the loaded .swf file on a webpage... The page loads a file called chat.swf, but i want to redirect to chat2.swf in the same directory in order to use the other file. I was told that this could work, but I have no idea how to use this API correctly and I cannot find any examples @_@
function incerceptChat(chat){
console.log( "onBeforeRequest", details );
if(chat.url == swf1){
chat = swf2;
}
}
that's my function that's supposed to change the URL, but I have been unable to get it to work (probably wrong syntax somewhere...) and I'm using this to listen:
chrome.experimental.webRequest.onBeforeRequrest.addListener(interceptChat, {"redirectUrl": chat});
回答1:
This is the basics of how to redirect a specific URL.
function interceptRequest(request) {
console.log('onBeforeRequest ', request.url);
if (request && request.url && request.url === 'http://example.org/') {
return { redirectUrl: 'http://www.google.com' }
}
}
chrome.experimental.webRequest.onBeforeRequest.addListener(interceptRequest, null, ['blocking']);
You can also replace null
with an object that further restricts the URL matching as Chrome will be more performant then having JS do it. Be careful not to be too greedy in what you redirect though as that can cause issues with general web browsing.
function interceptRequest(request) {
return { redirectUrl: 'http://example.com/chat2.swf' }
}
chrome.experimental.webRequest.onBeforeRequest.addListener(interceptRequest, { urls: ['http://example.com/swf'] }, ['blocking']);
来源:https://stackoverflow.com/questions/6447082/using-chrome-experimental-webrequest-api-to-change-loaded-file