2012-09-25 56 views
2

我正在用两种不同的内容脚本构建Safari扩展。一个脚本需要注入所有http页面(但不是https页面)。另一个只注入到google.com网页,而不考虑方案。内容脚本注入的多个白名单/黑名单?

为了实现这一点,我已经设置Extension Website Access到:

Extension Website Access

这应该意味着在一个较高的水平,在我的扩展内容脚本应该能够访问的所有页面。

为了获得更精细的控制,我随后将内容脚本注入到符合我的模式的URL中。

App = { 
    content: { 
    // Inject into unsecure pages. 
    whitelist: ['http://*/*'], 
    // But not into secure pages. 
    blackList: ['https://*/*'], 
    loc: safari.extension.baseURI + 'data/content.js' 
    }, 
    results: { 
    // Inject this script into all google.com pages 
    whiteList: ['http://*.google.com/*', 'https://*.google.com/*'], 
    // Surely I don't need a blacklist if I specify a whitelist? 
    blacklist: undefined, 
    loc: safari.extension.baseURI + 'data/results.js', 
    } 
}; 

// Inject the first content script. 
safari.extension.addContentScriptFromURL(App.content.loc, 
    App.content.whitelist, App.content.blacklist, false); 

// Inject the second content script. 
safari.extension.addContentStyleSheetFromURL(App.results.cssLoc, 
    App.results.whitelist, App.results.blacklist, false); 

问题是两个脚本都被注入到所有页面中。就好像我的白人和黑名单无所作为。我究竟做错了什么?

+0

我不知道这是否是问题的根源,但是您不应该在第一次注入命令中同时需要白名单和黑名单,因为这两套完全平铺空间。 – canisbos

回答

2

我在上面用大写字母在我whilelist /黑名单的定义:

App = { 
    content: { 
    blackList: ['https://*/*'], 
    }, 
    results: { 
    whiteList: ['http://*.google.com/*', 'https://*.google.com/*'] 
    } 
}; 

但是如果使用变量的非资本化的版本时,我通过列表到脚本注入功能。

safari.extension.addContentScriptFromURL(App.content.loc, App.content.whitelist, App.content.blacklist, false); 

这显然意味着,undefined正在传递到注入功能,而不是实际的白名单/黑名单。

+0

请使用您问题上的编辑链接添加其他信息。后回答按钮应该只用于问题的完整答案。 –

+0

我改进了我的答案,并投票结束了原来的问题。我希望这些变化令人满意。 –