Lambert_W
Lambert_W

Reputation: 165

My Greasemonkey script works when typed in the console, but not when I try to use it in Firefox

I am having trouble running a simple GreaseMonkey script I made to clean up the search results on DuckDuckGo.com. The code below simply removes the "Images" and "Videos" buttons from the results.

// ==UserScript==
// @name        Relevance
// @include     http://duckduckgo.com/*
// @include     https://duckduckgo.com/*
// @version     1
// @grant       none
// ==/UserScript==

function Relevant(){
    var killIt=["zcm__link  js-zci-link  js-zci-link--images  ", "zcm__link  js-zci-link  js-zci-link--videos  "];
    var el = document.getElementsByTagName("a");
    for (i = 0; i < el.length; i++) 
        {
            for (j=0;j<killIt.length; j++) 
                {
                    if (el[i].className==killIt[j]) 
                        {
                            el[i].parentNode.removeChild(el[i]);
                        }
                }
        }
};


$(window).load (Relevant());

The code works when typed in the console, but it doesn't work as a Greasemonkey script. I tried to adapt the solution found here (i.e., use $(window).load (Relevant());), but I'm still having trouble. This is my first attempt at making my own user script, so any help is much appreciated.

Upvotes: 0

Views: 446

Answers (1)

blackmiaool
blackmiaool

Reputation: 5344

As @charlietfl said, the $(window).load (Relevant()); invokes Relevant immediately, it should be changed to $(Relevant).

If it works, try this: https://wiki.greasespot.net/Metadata_Block#.40run-at

If this doesn't work, try to use setTimeout(Relevant,3000);

It's very usual that the code works in console doesn't work in greasemonkey, becase the doms may not be rendered when the greasemonkey script runs.

Upvotes: 1

Related Questions