交叉骑士 - 如何将脚本注入特定选项卡?以及如何重新加载特定选项卡
CrossRider - How do I inject script to specific tabs? And how do I reload specific tabs?
我们正在使用CrossRider为Internet Explorer开发扩展。我们也有Chrome,Firefox和Safari的扩展,但我们没有使用CrossRider进行这些扩展。我想知道我们如何将脚本注入到交叉骑士中的特定选项卡?在我们这样做之后,它也会被注入到稍后会打开的选项卡中吗?如果是,我们如何删除脚本,使其不会再注入任何选项卡?
这是脚本,我们必须为CrossRider添加一个案例。只有在 Safari 中,我们还会删除脚本,因为在 Chrome 和 Firefox 中,它不会添加到稍后将打开的选项卡中。如果它在 CrossRider 中确实如此,那么我们也必须在 CrossRider 中删除它。
Controller.showNotification = function() {
var possibleURLs = /(mail'.google'.com|mail'.yahoo'.com|mail'.live'.com|mail'.aol'.com|mail'.rambler'.ru)/gi;
var possibleURLsArray = ["http://mail.google.com/*", "https://mail.google.com/*", "http://*.mail.yahoo.com/neo/*", "https://*.mail.yahoo.com/neo/*", "http://*.mail.yahoo.com/mc/*", "https://*.mail.yahoo.com/mc/*", "http://*.mail.yahoo.com/dc/*", "https://*.mail.yahoo.com/dc/*", "http://*.mail.live.com/*", "https://*.mail.live.com/*", "http://*.webmail.aol.com/*/Suite.aspx", "http://*.webmail.aol.com/*/suite.aspx", "http://*.mail.aol.com/*/suite.aspx", "http://*.mail.aol.com/*/Suite.aspx", "http://mail.aol.com/*/suite.aspx", "http://mail.aol.com/*/Suite.aspx", "https://*.webmail.aol.com/*/Suite.aspx", "https://*.webmail.aol.com/*/suite.aspx", "https://*.mail.aol.com/*/suite.aspx", "https://*.mail.aol.com/*/Suite.aspx", "https://mail.aol.com/*/suite.aspx", "https://mail.aol.com/*/Suite.aspx", "http://mail.rambler.ru/mail/compose.cgi*"];
var possibleURLsScriptURL = Utils.getUrl("content/src/common/show_notification_script.js");
var possibleURLsScriptRelativeURL = Utils.getRelativeUrl("content/src/common/show_notification_script.js");
switch (Sys.platform) {
case 'chrome':
chrome.tabs.query({}, function(tabs) {
for (var i in tabs) {
if (tabs[i].url.match(possibleURLs) !== null) {
chrome.tabs.executeScript(tabs[i].id, {
file: possibleURLsScriptRelativeURL
});
}
}
});
break;
case 'safari':
safari.extension.addContentScriptFromURL(possibleURLsScriptURL, possibleURLsArray, [], true);
break;
case 'mozilla':
for (var i in tabs) {
if (tabs[i].url.match(possibleURLs) !== null) {
tabs[i].attach({
contentScriptFile: possibleURLsScriptURL
});
}
}
break;
case 'crossrider':
appAPI.dom.onDocumentStart.addJS({
resourcePath: possibleURLsScriptRelativeURL,
whitelistUrls: possibleURLs
});
break;
}
};
Controller.disableShowNotification = function() {
var possibleURLsScriptURL = Utils.getUrl("content/src/common/show_notification_script.js");
switch (Sys.platform) {
case 'safari':
safari.extension.removeContentScript(possibleURLsScriptURL);
break;
}
};
Utils.getUrl = function(filename, preferSecure) {
return WS.getURL(filename, preferSecure);
};
Utils.getRelativeUrl = function(filename) {
return WS.getRelativeUrl(filename);
};
/* Function to retrieve the relative URL/URI of a file in the platform's file system. */
WS.getURL = function(filename, preferSecure) {
if (typeof filename !== "string") {
filename = "";
} else if (filename.substr(0, 1) === "/") { /* Remove forward slash if it's the first character, so it matches with the base URLs of the APIs below. */
filename = filename.substr(1);
}
switch (Sys.platform) {
case 'mozilla':
if (typeof exports === 'undefined') { // Means we're in a content script.
return self.options.extensionURL + filename;
}
return require("sdk/self").data.url("../lib/"+filename);
case 'chrome':
return chrome.extension.getURL(filename);
case 'safari':
return safari.extension.baseURI + filename;
case 'web':
case 'conduit':
if (preferSecure && 'remote_secure' in WS.config.URLs.APIs) {
return WS.config.URLs.APIs.remote_secure + filename;
}
return WS.config.URLs.APIs.remote + filename;
case 'crossrider':
filename = filename.substr("content/".length);
if (filename.indexOf('png') !== -1) {
return appAPI.resources.getImage(filename);
}
return "resource://" + filename;
default:
return '../' + filename; /* Added temporarily as a fix for Node.js compatibility */
}
};
/* Function to retrieve the relative URL/URI of a file in the platform's file system. */
/* Currently this function is only defined for chrome and crossrider. */
WS.getRelativeUrl = function(filename) {
if (typeof filename !== "string") {
filename = "";
} else if (filename.substr(0, 1) === "/") { /* Remove forward slash if it's the first character, so it matches with the base URLs of the APIs below. */
filename = filename.substr(1);
}
switch (Sys.platform) {
case 'chrome':
return "/" + filename;
case 'crossrider':
filename = filename.substr("content/".length);
return filename;
}
};
我们如何在交叉骑士中重新加载特定选项卡?我们是否必须向选项卡发送一条消息,该消息将重新加载自身?或者是否可以从后台重新加载选项卡?
Controller.reloadAllEmailTabs = function() {
var possibleURLs = /(mail'.google'.com|mail'.yahoo'.com|mail'.live'.com|mail'.aol'.com|mail'.rambler'.ru)/gi;
switch (Sys.platform) {
case 'chrome':
chrome.tabs.query({}, function(tabs) {
for (var i in tabs) {
if (tabs[i].url.match(possibleURLs) !== null) {
chrome.tabs.reload(tabs[i].id);
}
}
});
break;
case 'mozilla':
for (var i in tabs) {
if (tabs[i].url.match(possibleURLs) !== null) {
tabs[i].reload();
}
}
break;
case 'safari':
var browserWindows = safari.application.browserWindows;
for (var i = 0; i < browserWindows.length; i++) {
var safari_tabs = browserWindows[i].tabs;
for (var j = 0; j < safari_tabs.length; j++) {
if (safari_tabs[j].url.match(possibleURLs) !== null) {
safari_tabs[j].url = safari_tabs[j].url;
}
}
}
break;
case 'crossrider':
appAPI.tabs.getAllTabs(function(tabs) {
for (var i = 0; i < tabs.length; i++) {
if (tabs[i].tabUrl.match(possibleURLs) !== null) {
appAPI.tabs.reload(tabs[i].tabId);
}
}
});
break;
}
};
我们的分机ID是43889。我正在使用Internet Explorer 11,但此扩展应该适用于所有版本的Internet Explorer。
更新:我从 Shlomo 的答案中为 CrossRider 添加了案例,但它们不起作用(它们在 CrossRider 中没有任何作用)。
如果我正确理解您的要求,可以总结如下:
- 如何将脚本注入特定选项卡?
- 如何将脚本注入新选项卡?
- 如何从注入更多选项卡中删除脚本?
- 如何从后台重新加载特定选项卡?
问题 1 和 2 可以使用 appAPI.dom.onDocumentStart.addJS 处理,将 whitelistUrls 属性指定为可能的 URL,而问题 4 可以使用 appAPI.tabs.getAllTabs 和 appAPI.tabs.reloadTab 的组合来实现。
至于问题 3,我不太清楚为什么要像为 Safari 所做的那样添加然后删除脚本,但如果目的是简单地阻止脚本在 Safari 上运行,您可以使用 appAPI.platform 围绕代码注入指定条件(参见示例)。
正如您将注意到的,所有这些都是根据需要在后台范围内实现的。如果我没有正确理解,请澄清每个问题,我会尽力提供帮助。
[披露:我是Crossrider员工]
背景.js:
appAPI.ready(function($) {
// get's all open tabs
appAPI.tabs.getAllTabs(function(tabs) {
for (var i=0; i<tabs.length; i++) {
// For tabs with matching URLs
if (tabs[i].tabUrl.match(possibleURLs) !== null) {
// Reload the tab
appAPI.tabs.reloadTab(tabs[i].tabId);
}
};
});
// For browsers other than Safari
if (appAPI.platform !== 'SF') {
// Inject script on tabs with matching URLs
appAPI.dom.onDocumentStart.addJS({
js: "alert('hello world!');",
whitelistUrls: possibleURLs
});
}
})
关于appAPI.dom.onDocumentStart.addJS,这确实会在加载时在所有与whitelistUrls匹配的页面上注入脚本,并且无法删除。如果需要更好地控制何时注入脚本,可以将 appAPI.tabs.executeScript 与 appAPI.tabs.onTabUpdate 结合使用,并在注入脚本之前添加一个条件,如下所示:
var DoNotLoad = false; // Your condition for not injecting script
appAPI.tabs.onTabUpdated(function(tabInfo) {
if (tabInfo.tabUrl.match(possibleURLs) !== null && !DoNotLoad) {
appAPI.tabs.executeScript({
tabId: tabInfo.tabId,
code: 'alert("Running script");'
});
}
});
- 如何在页面加载后更改“选定”选项
- 如何使新选择的html选项成为页面加载的第一选择
- 嵌套的引导选项卡未正确加载内容
- 在新的浏览器选项卡上加载新的aspx页面后调用JavaScript函数
- 如何使用选项useCSS set true为bxSlider加载onSlideAfter()事件
- 如何在页面加载angular js上设置从ajax调用中选择的选项
- 访问url[材质Ui+选项卡]时,Angular Ui路由器未加载视图
- 过滤”;溢价;页面加载选项使用混合
- 如何在打印前等待javascript文件加载twitter,并在打印后关闭选项卡
- PHP Jquery选项卡:内容加载到一个选项卡's面板
- 启动选项卡ajax在加载页面时加载内容
- 如果使用jquery在页面加载时未选择任何选项,如何禁用下拉列表
- Jquery选择选项并获取只工作一次的更改值(页面加载时)
- 选项卡只在加载时显示过多内容,点击后自行修复
- 通过javascript在同一个打开的选项卡中重新加载新的网页内容
- 在下拉列表中的页面加载中已经选择了一个选项,该选项也可以更改
- j查询选项卡未加载
- Jscroll不显示触发选项(加载更多选项)
- jQuery -通过选择选项加载图像
- 基于Select选项加载内容