0

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

H.B.
  • 166,899
  • 29
  • 327
  • 400
Aarish Ramesh
  • 6,745
  • 15
  • 60
  • 105
  • `xmlHttpRequest` *is* AJAX. – H.B. Mar 25 '18 at 16:01
  • @H.B. : yes right. Can you help me with the question ? – Aarish Ramesh Mar 26 '18 at 19:17
  • I could, probably, but i would just do what you should do: Study the GM documentation to understand the `xmlHttpRequest` function's API and then use it. – H.B. Mar 26 '18 at 19:20
  • @H.B. : I appreciate your sincere effort in helping me with the right way. I went through GM xmlHttpRequest doc but i don't see a way to implement long polling. I am relatively new to javascript. That's why. It would be great if you could guide me on how to accomplish this – Aarish Ramesh Mar 26 '18 at 19:22
  • Long polling is something that has to be supported by the server. There is nothing you can do about it on the client if the server does support it. – H.B. Mar 26 '18 at 19:23
  • okay i get it. Lemme rephrase my question. How do I implement GM script to periodically poll my server ? I know I can make the call using xmlHttpRequest. But how do i make the tamper monkey script execute periodically ? – Aarish Ramesh Mar 26 '18 at 19:27
  • 1
    Just use [`setInterval`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval). – H.B. Mar 26 '18 at 19:28
  • So basically I am wondering on how to implement a client side hack to implement long polling to perform the desired tweak I need to implement for the page – Aarish Ramesh Mar 26 '18 at 19:28
  • You can't implement long polling, it's just polling then. – H.B. Mar 26 '18 at 19:28
  • Yes you are right it's just polling @H.B. I will look into setInterval and implement it. Thanks a lot – Aarish Ramesh Mar 26 '18 at 19:29

1 Answers1

1

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
PMM
  • 176
  • 2
  • 12