【发布时间】:2013-11-18 07:37:39
【问题描述】:
我有一个简单的 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 书签生成器运行后,它运行良好。在 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 topic 和 github blog post about CSP,它们承认 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)
但不出所料,这不适用于书签,因为调用代码仍然来自浏览器。
简而言之 - 关于如何让这个小书签在 Firefox 上工作的任何想法?
【问题讨论】:
-
Firefox 69 和更新版本不再阻止具有严格 CSP (bugzilla.mozilla.org/show_bug.cgi?id=1478037) 的页面上的小书签,尽管有一些限制。
标签: javascript firefox bookmarklet content-security-policy