javascript - 脚本在 Greasemonkey 中有效,但在 Tampermonkey 中没有任何反应?

标签 javascript google-chrome greasemonkey tampermonkey

以下脚本在 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();

最佳答案

问题代码不应在任何浏览器中工作,您应该会在控制台中看到错误消息。

问题:

  1. document.getElementById('survey16') does not have a .hide() method .这是一个 jQuery 函数。
  2. removeSurvey() 应该是:

    function removeSurvey (jNode) {
        jNode.hide ();  //-- .hide is a jQuery function.
    }
    
  3. 除此之外,waitForKeyElements 调用与 removeSurvey 之间存在不匹配。
    在第一个中,您正在搜索具有 class survey16 的 div,但在第二个中,您试图删除具有 id 的元素>调查 16。是哪个?
  4. 作为一般规则,不要在使用 @require 的同时使用 @grant none,这通常会导致页面冲突和崩溃。 jQuery is especially bad.
  5. 此外,@grant none 在两种浏览器中的功能略有不同。使用 @require 时,请指定 @grant GM_addStyle,特殊情况和罕见情况除外。

关于javascript - 脚本在 Greasemonkey 中有效,但在 Tampermonkey 中没有任何反应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40870609/

相关文章:

javascript - 除非刷新页面,否则 Grease/tampermonkey 脚本无法正常工作

javascript - 将参数从 JQuery 发送到 Flask

javascript - jPlayer "duration"在 iO 上计算为 1,440 分钟(对于所有 mp3)

javascript - 在 angularJS 中使用 {{ }} 查看输出 Angular 变量名称中显示的加载时间

java - 为什么 Chrome 不渲染从套接字输出流获取的页面? java

javascript - 用户/Javascript 格式

javascript - 使引导菜单在加载时可见

facebook - 从安全 Canvas 页面链接到不安全页面不再在 Chrome 中工作

javascript - Chrome 版本 57.0.2987 中的 chrome.runtime.sendMessage 错误

javascript - 在提交表单之前更改数据