如何在Google Chrome扩展程序中重定向某些页面? [英] How to redirect some pages in Google chrome extension?

查看:1517
本文介绍了如何在Google Chrome扩展程序中重定向某些页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


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

函数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};
}

函数getLocation(href){
var l = document.createElement('a');
l.href = href;
return l;
};

因此,如果用户输入页面或点击该页面的链接,我想重定向他。
上面的代码工作得很好; 问题是当页面请求例如CSS然后这些CSS结束在用户屏幕上。






所以我想要的只是过滤请求到页面/ clicks ..如何做到这一点?

解决方案

通过设置 types to ['main_frame'] (使用 ['main_frame','sub_frame'] ,如果您还想包含框架)。



尽管之前的建议可以解决您的问题,但我建议您再去一步,摆脱 chrome.tabs.update 。使用 redirectUrl



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

  chrome.webRequest.onBeforeRequest.addListener(function(details){
var location = getLocation(details.url); // getLocation在问题中定义
var redirect_url ='http://some_redirect.com'+ location.pathname;
return {redirectUrl:redirect_url};
},{
types:['main_frame','sub_frame'],
urls:['*:// * / * some_pattern *']
},[''阻挡']);

注意:不要使用< all_urls> 如果您不准备处理非http请求。


I am using chrome.webRequest.onBeforeRequest in my background script to listen to all request a user is making. My extension now is just a manifest and this bg script.

I then check if the url matches a regex:

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;
};

So if a user enters a page or clicks on a link to that page, I'd like to redirect him. The code above works almost fine; the problem is when the page requests e.g. CSS then those CSS end up on the user screen.


So what I'd want is to only filter requests to pages / clicks.. how to do this?

解决方案

Limit event notifications to the top-level frame by setting types to ['main_frame'] (use ['main_frame', 'sub_frame'] if you want to include frames as well).

Although the previous suggestion will fix your problem, I suggest to go a step further and get rid of chrome.tabs.update. Use redirectUrl to replace the previous request with a redirect to the desired URL.

If your URL-pattern matching method is very simple, expressible as a match pattern, incorporate the filter in the "urls" key of the webRequest API filter. This will reduce your extension's impact on your browser's performance.

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']);

Note: Do not use <all_urls> if you are not prepared to handle non-http(s) requests.

这篇关于如何在Google Chrome扩展程序中重定向某些页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆