Search code examples
javascriptajaxxmlhttprequestpollinggreasemonkey-4

Implement polling with grease monkey script


I want to execute a tamper monkey script on facebook page which polls database periodically for data and performs some actions. I tried implementing polling with ajax and below is the code for it

(function poll() {
    setTimeout(function() {
           $.ajax({
                  url: "xyz",
                  headers: {
                  'Content-Type': 'application/json',
                  'x-apikey': apiKey,
                  'cache-control': 'no-cache'
                  },
                  type: "GET",
                  success: function(data) {

                  // check if null return (no results from API)
                  if (data == null) {
                        console.log('no data!');
                  } else {
                        console.log(data);                                          
                  },
                  dataType: "json",
                  complete: poll,
                  timeout: 2000
                  });
           }, 3000);
    })();

But when i execute the script, I get the below error

Refused to connect to 'xyz' because it violates the following Content Security Policy directive: "connect-src *.facebook.com facebook.com *.fbcdn.net *.facebook.net .spotilocal.com: .akamaihd.net wss://.facebook.com:* https://fb.scanandcleanlocal.com:* .atlassolutions.com attachment.fbsbx.com ws://localhost: blob: *.cdninstagram.com 'self' chrome-extension://boadgeojelhgndaghljhdicfkmllpafd chrome-extension://dliochdbjfkdbacpmhlcpmleaejidimm".

I understand that the error is because of content security policy directive defined by facebook.

Is there any way by which i can implement polling ? I also checked grease monkey GM.xmlHttpRequest but I could not find out a way to implement polling apart from using ajax.

I would appreciate if someone could help


Solution

  • I think the error you are receiving is related to cross domain policies. Greasemonkey/Tampermonkey userscripts' GM_xmlhttpRequest works cross domain, so you need to be using GM_xmlhttpRequest instead of $.ajax. I have taken a stab at rewriting your above code in the GM_xmlhttpRequest formulation, so you get a sense of how it translates:

    var pollTimer = setInterval(function() {
         try {
              GM_xmlhttpRequest({
                   method: "GET",
                   url: "xyz",
                   headers: {'Content-Type': "application/json",
                             'x-apikey':apiKey,
                             'cache-control': "no-cache"},
                   onload: function(data){
                        // check if null return (no results from API)
                        if (data === null) {  // <-- note that you should use === rather than == for checking against null
                            console.log('no data!');
                        } else {
                            console.log(data); // if data in json format, will instead need console.log(JSON.stringify(data)) here
                        }
                   },
                   onerror: function(err) {
                       console.log('crap!  an error! ' + err);
                   }
             });
             if (some condition exists that you would like polling to stop) {
                 clearInterval(pollTimer);
             }
        } catch(e) {};
    }, 3000);  // check every 3000 milliseconds