我正在使用Crossrider构建一些浏览器扩展。在呈现任何DOM元素之前,我需要在页面加载之前执行一些代码。这可能与Crossrider有关吗?
我尝试过使用appAPI.dom.onDocumentStart.addJS
在后台范围但没有运气。在我的代码执行之前,我仍然看到页面部分显示。
由于
更新详情
我正在尝试做的一个例子是将坏网站重定向到好网站的扩展程序。基本上,如果用户点击我们列表中不好的网站,它会将它们重定向到一个好的替代方案并给他们一个消息。我正在研究重定向部分,这里有一些示例代码。
在我的 background.js
中appAPI.ready(function($) {
appAPI.dom.onDocumentStart.addJS({resourcePath:'main.js'});
});
然后我的资源中有 main.js 文件
if(window.location.href == "http://www.bing.com/"){
console.log("You are at Bing... That's bad m'kay.");
window.location = "http://www.google.com";
}
在这种情况下,如果用户进入bing,他们会被重定向到谷歌,然后会得到一些有趣的消息。他们还会收到4chan,torrent网站等网站的警告。
所以目前如果我实现这个代码,然后去bing它会闪烁DOM然后重定向。我试图阻止这一点。谢谢!
我正在测试Win 7和Chrome
谢谢!
答案 0 :(得分:2)
@Dan感谢您提供其他信息,特别是您尝试实现的方案。
在这种情况下,您只需使用onBeforeNavigate方法即可实现目标,而无需在文档启动时运行。因此,根据您的 background.js 示例代码,解决方案如下所示:
appAPI.ready(function() {
var resourceArray = ['http://www.bing.com/'];
appAPI.webRequest.onBeforeNavigate.addListener(function(details, opaqueData) {
// Where:
// * details.pageUrl is the URL of the tab requesting the page
// * opaqueData is the data passed to the context of the callback function
// Redirect requests for blocked pages
for (var i = 0; i < opaqueData.length; ++i) {
if (details.pageUrl.indexOf(opaqueData[i]) !== -1) {
console.log("You are at Bing... That's bad m'kay.");
return { redirectTo: 'http://www.google.com' };
}
}
}, resourceArray); // resourceArray is the opaque parameter that is passed to the callback function
});
[披露:我是Crossrider员工]