Javascript CrossRider-如何将脚本注入特定选项卡?如何重新加载特定选项卡?

Javascript CrossRider-如何将脚本注入特定选项卡?如何重新加载特定选项卡?,javascript,browser-extension,crossrider,Javascript,Browser Extension,Crossrider,我们正在使用CrossRider开发Internet Explorer的扩展。我们也有针对Chrome、Firefox和Safari的扩展,但我们没有为这些扩展使用CrossRider。我想知道如何将脚本注入CrossRider中的特定选项卡?在我们这样做之后,它是否也会被注入到稍后打开的选项卡中?如果是,我们如何删除脚本,使其不会被注入任何其他选项卡 这是脚本,我们必须向CrossRider添加一个案例。只有在Safari中,我们才会删除该脚本,因为在Chrome和Firefox中,它不会添加

我们正在使用CrossRider开发Internet Explorer的扩展。我们也有针对Chrome、Firefox和Safari的扩展,但我们没有为这些扩展使用CrossRider。我想知道如何将脚本注入CrossRider中的特定选项卡?在我们这样做之后,它是否也会被注入到稍后打开的选项卡中?如果是,我们如何删除脚本,使其不会被注入任何其他选项卡

这是脚本,我们必须向CrossRider添加一个案例。只有在Safari中,我们才会删除该脚本,因为在Chrome和Firefox中,它不会添加到稍后打开的选项卡中。如果它在Crossride中存在,那么我们也必须在Crossride中移除它

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;
    }
};
我们如何在CrossRider中重新加载特定选项卡?我们是否必须发送一条消息到将重新加载自身的选项卡?或者可以从后台重新加载选项卡吗

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;
    }
};
Controller.reloadallemailtab=function(){
var possibleURLs=/(mail\.google\.com | mail\.yahoo\.com | mail\.live\.com | mail\.aol\.com | mail\.rambler\.ru)/gi;
交换机(系统平台){
案例“chrome”:
chrome.tabs.query({},函数(tabs){
用于(选项卡中的var i){
if(制表符[i].url.match(可能的url)!==null){
chrome.tabs.reload(tabs[i].id);
}
}
});
打破
“mozilla”案例:
用于(选项卡中的var i){
if(制表符[i].url.match(可能的url)!==null){
tabs[i].reload();
}
}
打破
“狩猎”一案:
var browserWindows=safari.application.browserWindows;
对于(变量i=0;i
我们的分机号码是43889。我使用的是Internet Explorer 11,但此扩展应适用于所有版本的Internet Explorer


更新:我从Shlomo的回答中添加了Crossride案例,但它们不起作用(它们在Crossride中不起任何作用)。

如果我正确理解您的要求,它们可以总结如下:

  • 如何将脚本注入特定选项卡
  • 如何将脚本注入新选项卡
  • 如何将脚本从注入更多选项卡中移除
  • 如何从后台重新加载特定选项卡
  • 问题1和2可以通过将whitelistUrls属性指定为可能的URL来处理,而问题4可以通过和的组合来实现

    至于问题3,我不太清楚为什么要像对待Safari那样添加然后删除脚本,但如果只是为了防止脚本在Safari上运行,可以在代码注入周围使用指定条件(参见示例)

    正如您将注意到的,所有这些都是在您需要的后台范围内实现的。如果我没有正确理解,请澄清每个问题,我会尽力帮助

    [披露:我是一名交叉骑手员工]

    background.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.ready(函数($){
    //把所有的标签都打开
    appAPI.tabs.getAllTabs(函数(选项卡){
    
    对于(var i=0;i关于appAPI.dom.onDocumentStart.addJS),这确实会在加载时将脚本注入与WhiteListURL匹配且无法删除的所有页面上。如果您需要更好地控制何时注入脚本,您可以在注入脚本之前结合使用并添加一个条件,例如:

    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");'
            });
        }
    });
    

    我们只在
    appAPI.platform==='IE'
    中使用CrossRider。如果我们使用
    appAPI.dom.onDocumentStart.addJS
    注入脚本,它会被注入到稍后打开并匹配白名单URL的选项卡中吗?如果不是,那么一切都正常,但是如果是,那么我们如何将它从注入到任何其他选项卡中移除?我们只实现了我在Safari中删除了它,因为我们没有找到一种方法将JavaScript仅注入Safari中的当前选项卡。
    appAPI.tabs.getAllTabs
    返回长度为0的选项卡。我也没有找到
    appAPI.tabs.reload
    ,仅
    appAPI.tabs.reloadTab
    。抱歉,我在提供示例代码片段时处于Chrome/Firefox模式,howevIE中不支持er getAllTabs。如果我正确理解您的场景,则在安装后第一次运行扩展时,您正在使用getAllTabs将代码注入现有页面(通过重新加载页面)(在后续运行中,代码由onDocument.addJS代码自动注入)但不幸的是,由于IE架构的原因,这在IE中是不可能的。另外,感谢您对reloadTab(更新代码片段以反映)的打字错误更正。Shlomo,那么我们如何在IE中实现重新加载特定选项卡和注入脚本呢?我用您的答案更新了我的代码(您可以看到我更新的问题)但两者都不适用于IE和CrossRider。