Tho*_*lor 6 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);
}
})();
Run Code Online (Sandbox Code Playgroud)
当我在Chrome或IE中运行它(通过Daring Fireball的JS小书签生成器运行它后,它可以正常工作。在Firefox中,它会生成内容安全策略错误:
[15:33:19.318] Content Security Policy: Directive inline script base restriction violated @ https://github.com/Groundwire/Campaign-Combiner
Run Code Online (Sandbox Code Playgroud)
我已经阅读了有关这个主题的SE问题,以及有关CSP的github博客文章,该文章承认CSP不应干扰小书签,但是在那时(2013年4月),“没有一个浏览器能正确地实现这一点。” 当前是否仍是Firefox仍然犯了这个错误,但是Chrome&IE却这样做了?
我还找到了一篇有关用户脚本和CSP的博客文章,作者在其中包括了github存储库中的代码,从而解决了该问题。我尝试过,将我的小书签修改为:
javascript:document.body.appendChild(document.createElement("script")).src="https://raw.github.com/tet3/GitHubInstallerBookmarklet/master/GHIBkmarklet.js";void(0)
Run Code Online (Sandbox Code Playgroud)
但毫不奇怪,这对于书签不起作用,因为调用代码仍然来自浏览器。
简而言之-关于如何使此书签在Firefox上运行的任何想法?
我也研究过这个问题,主要是在 Firefox 中。我不知道它能在 Chrome 中运行;这可能是最近的变化。如果 FF 不更改为将小书签识别为政策之外(理应如此!),则没有解决方法。脚本不会运行,完全停止,你已经死在水里了。
备择方案:
1.) 创建一个插件;或利用现有的插件(如 Greasemonkey)来运行用户脚本。
2.) 在 Web 控制台中运行代码。在 FF 中,按 CTRL+Shift+K 即可立即到达。
3.) FF 的开发人员暂存器也可以使用。如果将代码保存在文件中,则可以使用 Shift+F4(打开暂存器)> 文件 > 打开最近使用的文件 > 选择文件 > CTRL+R(运行)相对快速地访问它。
| 归档时间: |
|
| 查看次数: |
1247 次 |
| 最近记录: |