2

I'm trying to run a Tampermonkey script in Chrome on Facebook to customise my Facebook page.

I can run some simple script, but I can't load jQuery.

Refused to load the script 'https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js' because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' https://.facebook.com http://.facebook.com https://.fbcdn.net http://.fbcdn.net *.facebook.net *.google-analytics.com *.virtualearth.net .google.com 127.0.0.1: .spotilocal.com: 'unsafe-inline' 'unsafe-eval' https://.akamaihd.net http://.akamaihd.net *.atlassolutions.com chrome-extension://lifbcibllhkdhoafpjfnlhfpfgnpldfl".

This is my code:

// Loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js");
  script.addEventListener('load', function () {
    var script = document.createElement("script");
    script.textContent = "window.jQ1=jQuery.noConflict(true);(" + callback.toString() + ")();"; // jQ1 replaces $ to avoid conflicts.
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

function main() {
  // empty for now, but won't even run
}

addJQuery(main);

Is there a way for me to use jQuery?

I also tried pasting the source inline but got this error:

Uncaught ReferenceError: jQuery is not defined

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Aximili
  • 28,626
  • 56
  • 157
  • 216
  • 1
    That's the wrong way to use jQuery, anyway. For Tampermonkey (and whenever possible) [`@require` it](http://stackoverflow.com/q/12250892/331508). (Or, see the second approach in [this answer](http://stackoverflow.com/a/12751531/331508) if you want to keep maximum cross-platform support -- although the CSP makes that irrelevant in this case). If you do that, then Facebook's extra-tight CSP becomes irrelevant. – Brock Adams Dec 23 '14 at 11:18
  • 1
    Thanks Brock! I replaced it with @require and it solved the problem! – Aximili Dec 25 '14 at 09:12

0 Answers0