【发布时间】: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()
我采取的行动:
- 我检查了 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');
空
-
尝试通过在 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