Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Google chrome扩展中重定向某些页面?_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 如何在Google chrome扩展中重定向某些页面?

Javascript 如何在Google chrome扩展中重定向某些页面?,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在使用chrome.webRequest.onBeforeRequest在我的后台脚本中监听用户发出的所有请求。我的扩展现在只是一个清单和这个bg脚本 然后检查url是否与正则表达式匹配: chrome.webRequest.onBeforeRequest.addListener( checkRedirect, {urls: ["<all_urls>"]}, ["blocking"] ); function checkRedirect(details) {

我正在使用
chrome.webRequest.onBeforeRequest
在我的后台脚本中监听用户发出的所有请求。我的扩展现在只是一个清单和这个bg脚本

然后检查url是否与正则表达式匹配:

chrome.webRequest.onBeforeRequest.addListener(
     checkRedirect, {urls: ["<all_urls>"]}, ["blocking"]
);

function checkRedirect(details) {   
    var location = getLocation(details.url);

    if(/some_regex/.test(details.url)) {
        var redirect_url = 'http://some_redirect.com' + location.pathname;
        chrome.tabs.update({url: redirect_url});
    }

    return {cancel: false}; 
}

function getLocation(href) {
    var l = document.createElement('a');
    l.href = href;
    return l;
};
chrome.webRequest.onBeforeRequest.addListener(
checkRedirect,{URL:[“”]},[“阻止”]
);
函数检查重定向(详细信息){
var location=getLocation(details.url);
if(/some_regex/.test(details.url)){
var重定向\u url='0http://some_redirect.com'+location.pathname;
更新({url:redirect_url});
}
返回{cancel:false};
}
函数getLocation(href){
var l=document.createElement('a');
l、 href=href;
返回l;
};
因此,如果用户进入某个页面或单击指向该页面的链接,我想重定向他。 上面的代码几乎可以正常工作问题是当页面请求CSS时,这些CSS就会出现在用户屏幕上



所以我只想过滤页面/点击的请求。。如何做到这一点?

通过设置为
['main\u frame']
(如果还想包括框架,请使用
['main\u frame','sub\u frame']
),将事件通知限制在顶级框架中

虽然前面的建议可以解决您的问题,但我建议您进一步删除
chrome.tabs.update
。用于将上一个请求替换为指向所需URL的重定向

如果您的URL模式匹配方法非常简单,可以表示为,请将过滤器合并到WebRequestAPI过滤器的键中。这将减少扩展对浏览器性能的影响

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    var location = getLocation(details.url); // getLocation defined in question
    var redirect_url = 'http://some_redirect.com' + location.pathname;
    return { redirectUrl: redirect_url };
}, {
    types: ['main_frame', 'sub_frame'],
    urls: ['*://*/*some_pattern*']
}, ['blocking']);

注意:如果您不准备处理非http(s)请求,请不要使用

通过设置为
['main\u frame']
将事件通知限制为顶级帧(如果您还想包括帧,请使用
['main\u frame','sub\u frame']

虽然前面的建议可以解决您的问题,但我建议您进一步删除
chrome.tabs.update
。用于将上一个请求替换为指向所需URL的重定向

如果您的URL模式匹配方法非常简单,可以表示为,请将过滤器合并到WebRequestAPI过滤器的键中。这将减少扩展对浏览器性能的影响

chrome.webRequest.onBeforeRequest.addListener(function(details) {
    var location = getLocation(details.url); // getLocation defined in question
    var redirect_url = 'http://some_redirect.com' + location.pathname;
    return { redirectUrl: redirect_url };
}, {
    types: ['main_frame', 'sub_frame'],
    urls: ['*://*/*some_pattern*']
}, ['blocking']);

注意:如果您不准备处理非http请求,请不要使用

非常感谢。但是,如果您不准备处理非http(s)请求,那么“不使用”是什么意思呢@user3733425
包括其他方案,如
文件:
*、
ftp:
等。如果您只想拦截http(s)请求,则应使用
*://*
。方案通配符扩展为
http://*/*
https://*/*
。非常感谢。但是,如果您不准备处理非http(s)请求,那么“不使用”是什么意思呢@user3733425
包括其他方案,如
文件:
*、
ftp:
等。如果您只想拦截http(s)请求,则应使用
*://*
。方案通配符扩展为
http://*/*
https://*/*