Does Content Security Policy block bookmarklets?

后端 未结 4 1701
予麋鹿
予麋鹿 2020-12-31 01:30

Does Mozillas CSP block to execute Javascript from a bookmark by default?

Can it be configured to do so?

4条回答
  •  梦谈多话
    2020-12-31 02:04

    Yes, the CSP blocks bookmarklets in Mozilla Firefox. There is a bug about it.

    However, you can get around this restriction by injecting the JS code into an external CSS stylesheet, like my Top News Feed bookmarklet does:

    External CSS:

    #topnewsfeed { font-family: '(function(){/*payload*/})()'; }
    

    Bookmarklet JS:

    (function() {
        var a = document.createElement("link");
        a.rel = "stylesheet";
        a.href = "//niutech.github.io/topnewsfeed/topnewsfeed.css";
        a.onload = function() {
            var a = b.currentStyle ? b.currentStyle.fontFamily : document.defaultView.getComputedStyle(b, null).fontFamily;
            eval(a.replace(/^["']|\\|["']$/g, ""));
        };
        document.body.appendChild(a);
        var b = document.createElement("div");
        b.id = "topnewsfeed";
        document.body.appendChild(b);
    })()
    

    The bookmarklet loads a CSS file containing JS code, adds an element styled by this CSS, reads the element style attribute and eval the code.

提交回复
热议问题