以下脚本在 Firefox/Greasemonkey 中有效,但在 Chrome/Tampermonkey 中没有任何反应。
谁能看出为什么它在 Tampermonkey 中不起作用?
// ==UserScript==
// @name Example
// @namespace Example.com
// @description Example.com
// @include https://example.com/*
// @include http://example.com/*
// @version 1
// @grant none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require https://greasyfork.org/scripts/5392-waitforkeyelements/code/WaitForKeyElements.js?version=115012
// ==/UserScript==
window.onload = function(){
document.getElementById('close-cookies').click();
};
waitForKeyElements('div.survey16', removeSurvey);
function removeSurvey() {
document.getElementById('survey16').hide();
}
$('.chat-bot').hide();
最佳答案
问题代码不应在任何浏览器中工作,您应该会在控制台中看到错误消息。
问题:
document.getElementById('survey16')
does not have a.hide()
method .这是一个 jQuery 函数。removeSurvey()
应该是:function removeSurvey (jNode) { jNode.hide (); //-- .hide is a jQuery function. }
- 除此之外,
waitForKeyElements
调用与removeSurvey
之间存在不匹配。
在第一个中,您正在搜索具有 classsurvey16
的 div,但在第二个中,您试图删除具有 id的元素>调查 16
。是哪个? - 作为一般规则,不要在使用
@require
的同时使用@grant none
,这通常会导致页面冲突和崩溃。 jQuery is especially bad. - 此外,
@grant none
在两种浏览器中的功能略有不同。使用@require
时,请指定@grant GM_addStyle
,特殊情况和罕见情况除外。
关于javascript - 脚本在 Greasemonkey 中有效,但在 Tampermonkey 中没有任何反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870609/