use*_*584 5 html javascript redirect google-chrome google-chrome-extension
我想编写一个扩展程序,将所有网络流量重定向到特定的域名,比如wikipedia.org,转到一个中间页面,上面写着"哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇哇..........对这个?" 接着Yes和No按钮.
如果在域中符合"wikipedia.org"页面的条件,我如何回复特定的URL请求并用自定义页面替换所需的页面?
你可以用做webRequest
功能,后台页面,并定制页面yes和no按钮.例如,在后台页面中写一些类似的东西:
var URLStorage;
function interceptRequest(request)
{
if(request && request.url)
{
if(request.type == "main_frame") // new page/site is loading in main window
{
if(request.url.indexOf("wikipedia.org") > -1)
{
URLStorage = request.url;
return {redirectUrl: chrome.extension.getURL("confirmation.html")};
}
}
}
}
chrome.webRequest.onBeforeRequest.addListener(interceptRequest, {urls: ["*://*/*"]}, ['blocking']);
Run Code Online (Sandbox Code Playgroud)
此示例并未严格检查域中是否提及维基百科,但为清楚起见,我这样做了.在我的真实代码中,使用了一个特殊的类"URL",它解析传递的url并为其的每个部分提供属性,因此可以有选择地检查它们.
在confirmation.html
正好位置2按钮,并将它们绑定到适当的代码,例如,如果用户回答"是",则重定向到请求的站点.
$('#okbutton').click(function()
{
document.location.href = chrome.extension.getBackgroundPage().URLStorage;
});
Run Code Online (Sandbox Code Playgroud)
不要忘记在permissions
清单的部分提及"webRequest"和"webRequestBlocking" .
归档时间: |
|
查看次数: |
3516 次 |
最近记录: |