Firefox中使用CSP的网站上的Javascript书签

时间:2013-11-06 21:00:07

标签: javascript firefox bookmarklet content-security-policy

我有一个简单的Javascript书签,我将它放在一起,以针对外部工具运行适当的GitHub存储库的内容:

javascript:(function(){ 
    var isApex = false;
    var sourceLangs = document.getElementsByClassName('lang');
        for (var i = 0; i < sourceLangs.length; ++i) {
        var l = sourceLangs[i]; 
        if(l.innerHTML == 'Apex') {
            isApex = true;
            // alert('This is an Apex repo');
        }
    }
    if(location.hostname != 'github.com' || isApex == false) {
        alert('This is not a GitHub Apex repository!');
    }
    else {
         window.open('https://githubsfdeploy.herokuapp.com/app/githubdeploy'+location.pathname);
    }

})();

当我在Chrome或IE中运行它时(在通过Daring Fireball的JS bookmarklet构建器运行它之后,它运行正常。在Firefox中,它会生成内容安全策略错误:

[15:33:19.318] Content Security Policy: Directive inline script base restriction violated @ https://github.com/Groundwire/Campaign-Combiner

我已阅读this SE question on the topicgithub blog post about CSP,它承认CSP不应干扰书签,但当时(2013年4月),“没有一个浏览器认为这是正确的。 “目前的情况是Firefox仍然出错了,但Chrome&amp; IE吗?

我还发现了一篇关于用户脚本和博客的博文。 CSP,作者能够通过包含来自github repo的代码来解决这个问题。我尝试过,将我的书签修改为:

javascript:document.body.appendChild(document.createElement("script")).src="https://raw.github.com/tet3/GitHubInstallerBookmarklet/master/GHIBkmarklet.js";void(0)

但不出所料,这对于书签来说不起作用,因为调用代码仍然来自浏览器。

简而言之 - 关于如何让这个书签在Firefox上运行的任何想法?

3 个答案:

答案 0 :(得分:3)

我也看过这个问题,主要是在Firefox中。我不知道它可以在Chrome中运行;这可能是最近的变化。缺少FF更改以将bookmarklet识别为在策略之外(因为它应该!),没有解决方法。水不会运行,完全停止,你死在水里。

备选方案:

1。)创建一个插件;或者使用像Greasemonkey这样的现有插件来运行用户脚本。

2。)在Web控制台中运行代码。在FF中,CTRL + Shift + K可以让你快速到达。

3.。)FF的开发人员便笺也有效。如果将代码保存在文件中,则可以使用Shift + F4(打开暂存器)&gt;相对快速地访问它。文件&gt;打开最近的&gt;选择你的文件&gt; CTRL + R(运行)。

答案 1 :(得分:1)

作为CSP阻止bookmarklet的变通方法,您可以告诉您的bookmarklet加载外部CSS样式表,并在其中注入JS代码。这就是我的Top News Feed bookmarklet的作用。 See my other answer

答案 2 :(得分:1)

我创造了一种解决方法&#34;修复&#34;使用Greasemonkey用户脚本(在Firefox中)解决此问题。您现在可以在所有CSP和https://网站上安装bookmarklet,并将您的书签放在一个漂亮,易于编辑的库文件中,而不是单独压缩成书签。

请参阅:http://www.donnelly-house.net/programming/js/bookmarklets/bookmarklets.php