Lek*_*eyn 28 security firefox bookmarklet
今天我注意到由于内容安全策略 (CSP) 限制,我无法在https://github.com/上运行书签。有没有办法在 Firefox 中为书签禁用 CSP,而不是其他所有内容?
我注意到 中的security.csp.enable选项about:config,但这会完全禁用 CSP。激活书签时,控制台会记录以下消息:
Timestamp: 04/22/2013 02:39:05 PM
Warning: CSP WARN: Directive inline script base restriction violated
Source File: https://github.com/
Line: 0
Source Code:
javascript:...
Run Code Online (Sandbox Code Playgroud)
Github 说它应该按照规范工作,但没有浏览器能做到这一点:
https://github.com/blog/1477-content-security-policy#bookmarklets
您应该为此问题为您喜欢的浏览器打开一个错误,或者为它投票:
您可以尝试将书签转换为GreaseMonkey 用户脚本。它们在特权环境中运行,不受 CSP 约束。
然而,当然用户脚本和书签的意图是不同的 - 用户脚本自动运行,而书签按需运行。例如,您可以通过<button>在用户脚本中创建一个,将其附加到页面,并onclick在该按钮上设置一个事件侦听器来触发书签的代码来规避这一点。
代码应该是这样的:
// ==UserScript==
// @name Name
// @description Description
// @version 0.1
// @namespace example.Lekensteyn
// @grant none
// @include http*://github.com/*/*/commit/*
// ==/UserScript==
var myBookmarklet = function () {
// here goes the code of the bookmarklet
};
var newButton = document.createElement('button');
newButton.innerHTML = 'Execute my bookmarklet';
newButton.addEventListener('click', function(evt) {
myBookmarklet();
});
document.getElementById('someElement').appendChild(newButton);
Run Code Online (Sandbox Code Playgroud)
几乎完全取自我的用户脚本,它也针对 GitHub。您可以使用debugger;脚本中的关键字在Firebug 中调试用户脚本。
但是请注意,Firebug 本身现在也受 CSP 约束,因此您不能在控制台中执行代码(但您可以在“只读”模式下检查您的用户脚本)。这在这个 bug 中得到了解决。
| 归档时间: |
|
| 查看次数: |
15333 次 |
| 最近记录: |