【问题标题】:Script works in Greasemonkey, but nothing happens in Tampermonkey?脚本在 Greasemonkey 中有效,但在 Tampermonkey 中没有任何反应?
【发布时间】:2016-11-30 03:02:29
【问题描述】:

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

【问题讨论】:

  • 如果你放一些console.log,它会登录到控制台吗?如果你删除一些@require

标签: javascript google-chrome greasemonkey tampermonkey


【解决方案1】:

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

问题:

  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 survey16 的元素。是哪个?
  4. 一般来说,在使用@require的同时不要使用@grant none,这通常会导致页面冲突和崩溃。 jQuery is especially bad.
  5. 另外,@grant none 在两种浏览器中的功能略有不同。使用 @require 时,请指定 @grant GM_addStyle,特殊情况和罕见情况除外。

【讨论】:

  • 太棒了!你知道调试jNode 包含的内容的方法吗?我不认为您为此使用console.log
  • jNode 始终是标准的jQuery object。而且,是的,您可以 console.log 它。
猜你喜欢
  • 2016-01-02
  • 2023-01-25
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-16
  • 1970-01-01
相关资源
最近更新 更多