https://chrome.google.com/webstore/上的Chrome扩展程序内容脚本

时间:2012-07-23 13:20:43

标签: google-chrome google-chrome-extension

Chrome是否阻止访问网上论坛网址?

我想制作一个在+1按钮旁边显示类似按钮的扩展程序,但看起来内容脚本无效https://chrome.google.com/webstore/ *

这是真的吗?

1 个答案:

答案 0 :(得分:29)

TL; DR无法通过扩展程序编写Webstore脚本,以及之前允许您执行此操作的标记(--allow-scripting-galleryhas been removed in Chrome 35

Chrome扩展程序无法执行内容脚本/在Chrome网上应用店中插入CSS。这在the source code,在函数IsScriptableURL中明确定义(单击上一个链接以查看完整逻辑)。

  // The gallery is special-cased as a restricted URL for scripting to prevent
  // access to special JS bindings we expose to the gallery (and avoid things
  // like extensions removing the "report abuse" link).
  // TODO(erikkay): This seems like the wrong test.  Shouldn't we we testing
  // against the store app extent?
  GURL store_url(extension_urls::GetWebstoreLaunchURL());
  if (url.host() == store_url.host()) {
    if (error)
      *error = manifest_errors::kCannotScriptGallery;
    return false;
  }

manifest_errors::kCannotScriptGallery定义为here

const char kCannotScriptGallery[] =
    "The extensions gallery cannot be scripted.";

使用chrome.tabs.executeScript在Web Store选项卡中注入脚本时,可以在后台页面的控制台中查看错误。例如,打开https://chrome.google.com/webstore/,然后在扩展的后台页面中执行以下脚本(通过控制台,进行实时调试):

chrome.tabs.query({url:'https://chrome.google.com/webstore/*'}, function(result) {
    if (result.length) chrome.tabs.executeScript(result[0].id, {code:'alert(0)'});
});