Search code examples
javascriptgoogle-chromegreasemonkeytampermonkey

Script works in Greasemonkey, but nothing happens in Tampermonkey?


The below script works in Firefox/Greasemonkey, but nothing happens in Chrome/Tampermonkey.

Can anyone see why it doesn't work in 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();

Solution

  • The question code should not work in either browser and you should see error messages in the consoles.

    Problems:

    1. document.getElementById('survey16') does not have a .hide() method. That's a jQuery function.
    2. removeSurvey() should be:

      function removeSurvey (jNode) {
          jNode.hide ();  //-- .hide is a jQuery function.
      }
      
    3. EXCEPT, there is a mismatch between the waitForKeyElements call and removeSurvey.
      In the first you are searching for a div with class survey16, but in the second you are trying to delete an element with the id survey16. Which is it?
    4. As a general rule, don't use @grant none when also using @require, this usually leads to page conflicts and crashes. jQuery is especially bad.
    5. Also, @grant none functions slightly differently in both browsers. When using @require, specify @grant GM_addStyle except in special, and rare, cases.