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

chi*_*ngo 24 google-chrome google-chrome-extension

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

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

真的吗?

Rob*_*b W 29

TL; DR Webstore不能通过扩展编写脚本,并且之前允许您执行该操作的标志(--allow-scripting-gallery)已在Chrome 35中删除.

Chrome扩展程序无法执行内容脚本/在Chrome网上应用店中插入CSS.这在源代码中,在函数中明确定义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;
  }
Run Code Online (Sandbox Code Playgroud)

manifest_errors::kCannotScriptGallery在这里定义:

const char kCannotScriptGallery[] =
    "The extensions gallery cannot be scripted.";
Run Code Online (Sandbox Code Playgroud)

当您使用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)'});
});
Run Code Online (Sandbox Code Playgroud)

  • @kzahel我刚刚解决了这个bug.您应该能够再次使用`--allow-scripting-gallery`(至少使用Canary版本). (2认同)