【问题标题】:jQuery seems to not be loaded when using Chrome Web Console使用 Chrome Web 控制台时似乎未加载 jQuery
【发布时间】:2016-11-09 01:54:21
【问题描述】:

以下脚本通过 Chrome Web 控制台成功运行了几个月,但突然无法正常工作。

// 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.");
}

抛出的错误是:

未捕获的 DOMException:无法在“文档”上执行“querySelector”:“[object HTMLDocument]”不是有效的选择器。

违规行是:

$(文档).ready()

我采取的行动:

  1. 我检查了 jQuery 是否已正确加载。以下命令的结果让我相信 jQuery 没有正确加载......也许我错了?

命令 1

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

未捕获的 DOMException:无法执行“querySelector” 'Document': '[object HTMLDocument]' 不是有效的选择器。

命令 2

console.log($())

命令 3

$().jquery

未捕获的类型错误:无法读取 null(...) 的属性“jquery”

命令 4

jQuery.jquery

Uncaught ReferenceError: jQuery is not defined(…)

命令 5

$('.class');


  1. 尝试通过在 Web 浏览器中运行以下代码来加载 jQuery:

    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);

得到这个错误:

VM711:3 拒绝加载脚本 'https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js' 因为它违反了以下内容安全政策指令: 呜呜呜。

一直在网上搜索解决方案,但感觉自己陷入了一个深坑。 是jquery没有加载吗? 是不是我正在处理数据的站点增加了新的安全层来阻止我的自动化?

我很茫然。提前致谢。

【问题讨论】:

  • 在我看来,页面正在使用 $ 做其他事情。试试console.log(jQuery).. 如果它显示为一个函数可以将你所有的$ 代码包装在一个 IIFE 中
  • 为了绕过内容安全策略,从 google url 中删除协议,使其仅以 // 开头或使用 http
  • 您在该行中的代码对我来说似乎很奇怪,不是吗:$(document).ready(function(){ /*Code here*/ });
  • @njoye 也可以是命名函数
  • @charlietfl 你的console.log(jQuery) 命令似乎有效。在 Web 控制台中运行它后,它没有抛出错误或任何其他消息。重新加载了我的代码,$(document).ready() 没有抛出任何错误。将补充说 Chrome 过去一直善变。从我发布问题到现在,浏览器中的某些条件或状态可能发生了变化。我将更新此内容以分享我的发现。谢谢你的帮助!!

标签: jquery google-chrome console domexception


【解决方案1】:

您发布了答案。 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 没有正确加载。尝试将 https://ajax 更改为 //ajax

【讨论】:

  • 谢谢!!你的回答很清楚我需要改变什么。基于其他一些建议,我认为他们指的是我浏览器中的 url,而不是我用来加载 jQuery 的代码中的 url。上面 charlietfl 的回答似乎有效,但可能是由于其他一些条件变化。我希望这个错误会重新出现。如果是这样,我将更彻底地测试这两种方法并分享我的发现。希望其他人能从中受益。
【解决方案2】:

以前有人问过这个问题,也许以下问题的答案会解决您的问题:

A subsite was referencing an older version of jQuery

【讨论】:

  • 如果您只是要参考上一个问题,请将其标记为重复,不要发布仅链接的答案。
  • @WSimpson 感谢您建议该线程。实际上,在提交我的之前,我读了好几遍。我的情况类似,错误消息是相同的。但是,条件不同,建议的行动方案不适用于我的情况。我希望其他有我问题的人可以从这里提供的答案中受益。
猜你喜欢
  • 2019-10-29
  • 2020-06-25
  • 2014-04-22
  • 2018-08-22
  • 1970-01-01
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 2011-06-15
相关资源
最近更新 更多