Search code examples
javascriptgreasemonkeyuserscripts

Greasemonkey script to hide stackoverflow posts?


I found myself reading the same questions over and over, so I wanted a way to hide questions.

I have a script to does what is suppose to do, however it cripples existing javascript, such as the upvote button and adding tags when asking questions. Does anyone know why this is happening, or how to fix it?

Edit: oh, in the error console I am getting:

Error: $ is not a function
Source File: http://cdn.sstatic.net/js/stub.js?v=b7084478a9a4
Line: 1

Edit2:

The solution

(fixed @17/06/2014)

// ==UserScript==
// @name           StackOverflowHidePosts
// @namespace      StackOverflowHidePosts
// @description    Allows you to hide questions on Stack Overflow.
// @include        http://stackoverflow.com/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js
// ==/UserScript==

var idListString = GM_getValue('idList', '');
var idList = idListString.split(',');
GM_setValue('idList', idList.join(','));

function getId (idString)
{
    return idString.split('-')[2];
}

function removeQuestion (e)
{
    var id = getId(e.data.questionSummaryDiv.id);

    $(e.data.questionSummaryDiv).hide(250);

    idList.push(id);

    setTimeout(function() {
        GM_setValue('idList', idList.join(','));
    }, 0);

    return false;
}

$('div.question-summary').each(function (index, questionSummaryDiv)
{
    var id = getId(questionSummaryDiv.id);

    if (idList.indexOf(id) != -1)
    {
        $(questionSummaryDiv).hide();

        return;
    }

    var link = $('<a><em>(Hide Post)</em></a>');

    link.attr('href', '#' + questionSummaryDiv.id);

    link.click({questionSummaryDiv: questionSummaryDiv}, removeQuestion);

    $('div.started', questionSummaryDiv).append(link);
});

Solution

  • Never inject JS if you don't have to, and never use the page's jQuery in FF GM -- that's the main source of errors in this case.

    The entire script should be:

    // ==UserScript==
    // @name           StackOverflowImTooStupidMarker
    // @namespace      StackOverflowImTooStupidMarker
    // @description    Allows you to hide questions on Stack Overflow when you can't answer them.
    // @include        http://stackoverflow.com/*
    // @require        http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
    // ==/UserScript==
    
    var idListString = GM_getValue('idList', '');
    var idList = idListString.split(',');
    GM_setValue('idList', idList.join(','));
    
    function getId (idString)
    {
        return idString.split('-')[2];
    }
    
    function removeQuestion (e)
    {
        var id = getId(e.data.questionSummaryDiv.id);
    
        $(e.data.questionSummaryDiv).hide(250);
    
        idList.push(id);
    
        setTimeout(function() {
            GM_setValue('idList', idList.join(','));
        }, 0);
    
        return false;
    }
    
    $('div.question-summary').each(function (index, questionSummaryDiv)
    {
        var id = getId(questionSummaryDiv.id);
    
        if (idList.indexOf(id) != -1)
        {
            $(questionSummaryDiv).hide();
    
            return;
        }
    
        var link = $('<a><em>(Too Stupid)</em></a>');
    
        link.attr('href', '#' + questionSummaryDiv.id);
    
        link.click({questionSummaryDiv: questionSummaryDiv}, removeQuestion);
    
        $('div.started', questionSummaryDiv).append(link);
    });