Chrome扩展程序重定向到带有参数的网址 [英] Chrome Extension to Redirect to URL with Parameter
本文介绍了Chrome扩展程序重定向到带有参数的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图创建一个Chrome扩展程序,该扩展程序会在URL的末尾匹配给定模式( *://*.mydomain.com/s/*
)。下面是我有的清单文件和后台脚本,但我无法使它工作。我做错了什么?
I'm attempting to create a Chrome extension which will add a parameter to the end of a URL if the URL matches a given pattern (*://*.mydomain.com/s/*
). Below is the manifest file and background script I have, but I cannot get it working. What am I doing wrong?
manifest.json:
manifest.json:
{
"manifest_version": 2,
"name": "Search Grid View",
"version": "0.1",
"description": "Changes MyDomain.com search to grid view by default",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"webRequest",
"*://*.mydomain.com/s/*",
"webRequestBlocking"
]
}
background.js:
background.js:
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var currentUrl = tabs[0].url;
var newUrl = currentUrl + "&style=gridview"
return { redirectUrl: newUrl};
},
{
urls: [
'*://*.mydomain.com/s/*'
],
types: ['main_frame']
},
['blocking']);
在此先感谢您的任何建议!
Thanks in advance for any advice!
推荐答案
- 使用调试器 - 在chrome:// extensions页面上点击你的扩展的
后台页面
,并切换到来源
面板。 - 要使用 onBeforeRequest 的回调参数
- 检查网址是否已被修改
- Use debugger - click your extension's
background page
on chrome://extensions page and switch to theSources
panel. - To obtain the url use onBeforeRequest's callback parameter
- Check if the url is already modified.
>
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
return {
redirectUrl: details.url +
(details.url.indexOf("?") == -1 ? "?" : "") +
(details.url.indexOf("&style=gridview") == -1 ? "&style=gridview" : "")
};
},
{urls: ['*://*.mydomain.com/s/*'], types: ['main_frame']},
['blocking']
);
这篇关于Chrome扩展程序重定向到带有参数的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文