2

The following script run successfully via the Chrome Web Console for months is suddenly not working.

// Override site's disabling of the console.log function
console.log = console.__proto__.log

// get the DOM ready to process
$(document).ready()
function doThisThing(){
  window._waitWindow = setInterval(function(){

    // start looking for items not 'ready to go'
    items = $("div.Catalogitem-content");
    $.each(items, function(index){

           if($(items[index]).find(".Catalogitem-stager").text().includes("ready to go") || index < lastitemCount){
              $(this).css("background-color", "#84f784");
            } else {
                $(this).css("background-color", "#ff7089");
                $(this).find(".engage-cycle-btn").click();
              }
         });
        window.scrollTo(0, document.body.scrollHeight);
    }, 10000);
  return window._waitWindow;
}

function stopit() {
  clearInterval(window._waitWindow);
  console.log("Just executed clearInterval.");
}

The error thrown is:

Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '[object HTMLDocument]' is not a valid selector.

The offending line is:

$(document).ready()

Actions I took:

  1. I checked to see if jQuery had been loaded properly. The results of the following commands led me to believe jQuery wasn't loaded properly... maybe I'm wrong?

Command 1

$(document).ready(function($){ });

Uncaught DOMException: Failed to execute 'querySelector' on 'Document': '[object HTMLDocument]' is not a valid selector.

Command 2

console.log($())

null

Command 3

$().jquery

Uncaught TypeError: Cannot read property 'jquery' of null(…)

Command 4

jQuery.jquery

Uncaught ReferenceError: jQuery is not defined(…)

Command 5

$('.class');

null


  1. Tried to load jQuery by running the following code in the Web Browser:

    var jq = document.createElement('script'); jq.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"; document.getElementsByTagName('head')[0].appendChild(jq);

Got this error:

VM711:3 Refused to load the script 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js' because it violates the following Content Security Policy directive: blah blah blah.

Been combing the internet for a solution, but feel I'm in a deep rabbit hole. Is it that jquery's not loaded? Is it that the site I'm processing data from has added a new layer of security to thwart my automation?

I'm at a loss. Thanks in advance.

rabbitholefinder
  • 129
  • 2
  • 13
  • Sounds to me like page is using `$` for something else. Try `console.log(jQuery)`.. If it shows it as a function can wrap all your `$` code in an IIFE – charlietfl Jul 06 '16 at 21:08
  • Also to get around the Content Security Policy remove the protocol from google url so it just starts with `//` or use `http` – charlietfl Jul 06 '16 at 21:10
  • Your code in that line seems odd to me, isn't it: `$(document).ready(function(){ /*Code here*/ });` ? – njoye Jul 06 '16 at 21:12
  • @njoye can be a named function also – charlietfl Jul 06 '16 at 21:14
  • @charlietfl your `console.log(jQuery)` command seemed to work. It didn't throw an error or any other message after running it in the web console. Reloaded my code and no error thrown from `$(document).ready()`. Will add that Chrome has been fickle in the past. It's possible that some condition or state in the browser had changed between the time I posted the question and now. I'll update this to share my findings. Thank you for your help!! – rabbitholefinder Jul 07 '16 at 13:49

2 Answers2

2

You posted the answer. Refused to load the script 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js' because it violates the following Content Security Policy - jQuery is not loading properly. Try changing https://ajax to just //ajax

Michael Hommé
  • 1,696
  • 1
  • 14
  • 18
  • 1
    Thank you!! Your answer made it very clear what I needed to change. Based on some other recommendations, i thought they were referring to the url in my browser, not the url in the code I was using to load jQuery. Answer from charlietfl above seemed to have worked, but could be due to some other condition change. I expect this error will resurface. If it does, I'll test both approaches more thoroughly and share my findings. Hope others can benefit from this. – rabbitholefinder Jul 07 '16 at 14:10
0

This has been asked before, perhaps the answer to the following question will address your issue:

A subsite was referencing an older version of jQuery

Community
  • 1
  • 1
WSimpson
  • 460
  • 4
  • 7
  • If you're just going to refer to a previous question, flag it as a duplicate, don't post a link-only answer. – Barmar Jul 06 '16 at 21:49
  • @WSimpson Thanks for suggesting that thread. Actually, I read that one several times before submitting mine. My scenario is similar in that the error message is the same. However, the conditions are different and the suggested course of action was not applicable in my case. I hope others with my issue can benefit from the answers provided here. – rabbitholefinder Jul 07 '16 at 13:57