【问题标题】:chrome extension not working for secure sites httpschrome 扩展不适用于安全站点 https
【发布时间】:2011-03-20 19:19:59
【问题描述】:

我编写了一个 chrome 扩展,但它似乎不适用于 https 网站。它目前是一个将脚本注入页面的背景页面。它也运行 jquery 和一些库。到目前为止,我发现这样做的唯一方法是运行一个后台页面,并使用 chrome.tabs.executescript。如果有人知道更好的方法,那也会有所帮助。

我已经添加了对 http 和 https 站点的权限,所以我认为这就足够了。请有人帮忙,谢谢。

清单:

 {
  "name": "My First Extension",  
  "version": "1.0",  
  "description": "The first extension that I made.",  
  "background_page": "popup.html",  
  "permissions": ["tabs", "http://*/*", "https://*/*"]  
 }  

popup.html

<script type="text/javascript">  

chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab)  
{  
    if(changeInfo.status == "loading")  
    {  
        chrome.tabs.insertCSS(null, { file: "jquery-ui-1.8.10.custom.css" }, null);  
        chrome.tabs.executeScript(null, { file: "jquery.min.js" }, null);  
        chrome.tabs.executeScript(null, { file: "jquery-ui-1.8.10.custom.min.js" }, null);  
        chrome.tabs.executeScript(null, { file: "jquery.hotkeys-0.7.9.min.js" }, null);  
        chrome.tabs.executeScript(null, { file: "custom.js" }, null);  
    }  
})  

</script>  

文件 custom.js 是我进行编码的地方。

谢谢

【问题讨论】:

  • 我认为 Google 会限制您这样做。

标签: google-chrome-extension


【解决方案1】:

尝试将“conent_scripts”添加到 manifest.json:

"content_scripts": [
    {
            "matches": ["http://*/*", "https://*/*"],
            "css": ["empty.css"]
    }]

您必须指定“css”或“文件”。在我的插件中,脚本是动态加载的,所以我只使用了一个虚拟 css 文件。

另请参阅:http://code.google.com/chrome/extensions/content_scripts.html

【讨论】:

  • 我已经试过了,很遗憾,这不起作用。也许 Blender 是对的,也许谷歌限制它在 https 网站上运行。很遗憾,因为我正在编写的扩展是一个弹出窗口,在热键上说 crtl+?尚未决定,将显示一个弹出窗口,您可以在其中键入新的 url,按 enter 并将页面更改为该站点。它适用于非安全站点。当您进行全屏(F11)时,这将非常有帮助。我希望在我完成后发布它。
  • 我刚刚检查了我的弹出窗口并在控制台中执行了chrome.tabs.executeScript(null, { code: "alert(document.title);" }, null)。这对 https 站点有效。您是否尝试过从调试器窗口运行它?异常消息大部分时间都很有用。
  • 我刚刚用我的扩展再次测试。看来我可以注入简单的脚本。 IE。 debugger; 工作。但是,如果我注入一组更复杂的脚本(jquery、jquery-ui 和一些 custom.js,它会显示一个弹出对话框),它就会默默地失败。没有错误,但也没有任何反应。
  • 嗨,是的,我的也与 Facebook 合作。你可以试试你的谷歌邮箱吗?例如mail.google.com/mail/?shva=1#inbox - 我的在那个页面上不起作用
  • 我没有 gmail 帐户。它可以在注册页面上使用。很可能脚本注入工作正常。也许页面的 JavaScript 会阻止某些事件冒泡到您的处理程序。就像我的对话框不适用于 。您是否尝试在custom.js 中添加一些alert('foo');
【解决方案2】:

可能依赖项有问题。所有对 executeScript 的调用都是异步的。因此,当您开始注入 jquery 热键时,您不能假设已注入 jquery。你最好使用这样的东西:

var runScripts = function(tabId, scripts, cb) {
        var current = scripts.shift();
        if (current) {
            chrome.tabs.executeScript(tabId, {file: current}, function(a) {
                console.log("Finished running script:", current);
                runScripts(tabId, scripts, cb);
            });
        } else {
            cb();
        }
    };

chrome.tabs.insertCSS(null, {file: "jquery-ui-1.8.10.custom.css"}, function () {
    runScripts( null, ["jquery.min.js", "jquery-ui-1.8.10.custom.min.js", "jquery.hotkeys-0.7.9.min.js", "custom.js"], function() {}); 
});

【讨论】:

  • 我还没有尝试过,但我认为答案是您之前提到的,即网站很复杂 - 并且其中包含框架集。我的扩展程序确实适用于 https,只是 google 邮件在站点中有框架。
  • 我是这么认为的。我假设 jquery-hotkeys-plugin / your custom.js 不适用于框架集。我不得不承认我对镜框不是很精通,因为我总是试图避免它们。无论如何,我认为您必须将处理程序附加到 $("body") 的每个帧。
猜你喜欢
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多