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

寵の児 提交于 2019-12-01 08:40:48

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