using chrome.experimental.webRequest API to change loaded file?

≡放荡痞女 提交于 2020-01-11 06:21:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!