是否可以使用Chrome扩展程序重写网址(使用额外的参数) [英] Is it possible to rewrite url (with extra parameters) with a Chrome extension

查看:159
本文介绍了是否可以使用Chrome扩展程序重写网址(使用额外的参数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户输入的url(在页面加载之前)附加一些额外的参数。例如,如果用户输入 www.google.com ,我希望可以这样做吗? 追加?q =查询到网址(最终: www.google.com?q=query



感谢

解决方案

webRequest API可能就是你需要的,这段代码放在你的后台页面:

  chrome.webRequest.onBeforeRequest.addListener(
函数(详情){
if(details.url ==http://www.google.com/ )
return {redirectUrl:http://www.google.com/?q=defaultquery};
},
{url:[http://www.google .com / *]},
[blocking]);

这是一个非常具体的规则,通过 http://www.google.com/?q将访问重定向到 http://www.google.com/ = defaultquery ,但我认为你可以看到如何将其扩展到incl ude更多功能。



请注意,这将重新路由所有尝试达到 http://www.google.com / code>,包括Ajax请求和iframe。



根据文档,您需要添加 webRequest webRequestBlocking 权限,以及您计划拦截的每个主机的主机权限:

 permissions:[
webRequest,
webRequestBlocking,
*://*.google.com/,
。 ..
],


I am trying to append few extra parameters to the url that user typed (before the page gets loaded). Is it possible to do?

For example, if user types www.google.com, I would like to append ?q=query to url (final: www.google.com?q=query.

Thanks

解决方案

The webRequest API might be what you need. This code goes in your background page:

chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        if( details.url == "http://www.google.com/" )
            return {redirectUrl: "http://www.google.com/?q=defaultquery" };
    },
    {urls: ["http://www.google.com/*"]},
    ["blocking"]);

This is an extremely specific rule that redirects visits to http://www.google.com/ with http://www.google.com/?q=defaultquery, but I think you can see how to expand it to include more functionality.

Note that this will reroute all attempts to reach http://www.google.com/, including Ajax requests and iframes.

Per the documentation, you will need to add the webRequest and webRequestBlocking permissions, along with host permissions for every host you plan to intercept:

"permissions": [
    "webRequest",
    "webRequestBlocking",
    "*://*.google.com/",
    ...
],

这篇关于是否可以使用Chrome扩展程序重写网址(使用额外的参数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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