Greasemonkey 脚本隐藏 stackoverflow 帖子?

发布于 2024-12-14 05:24:57 字数 1602 浏览 3 评论 0原文

我发现自己一遍又一遍地阅读相同的问题,所以我想要一种隐藏问题的方法。

我有一个脚本可以完成应该做的事情,但是它削弱了现有的 JavaScript,例如投票按钮和在提问时添加标签。有谁知道为什么会发生这种情况,或者如何解决它?

编辑:哦,在错误控制台中我得到:

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

Edit2:

解决方案

(已修复@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);
});

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);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-12-21 05:24:57

如果没有必要,切勿注入 JS,并且切勿在 FF GM 中使用页面的 jQuery —— 这是这种情况下错误的主要来源。

整个脚本应该是:

// ==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);
});

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);
});
时光与爱终年不遇 2024-12-21 05:24:57

该脚本首先尝试包含 jQuery:

(function()
{
    if (typeof unsafeWindow.jQuery == 'undefined')
    {
        var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement, GM_JQ = document.createElement('script');

        GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
        GM_JQ.type = 'text/javascript';
        GM_JQ.async = true;

        GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
    }        
    GM_wait();
})();

这个问题是 jQuery 肯定会首先加载到 Stack Overflow 上...如果它不存在,那么您就会遇到更大的问题。整个 jQuery 替换不应该发生,因为它既影响已经注册的插件(然后是核武器),又使用 Stack Exchange 目前使用的更新版本的 jQuery,这也意味着其他潜在的重大变化。

由于该脚本不需要任何最新功能,因此上面的整个块应该只是:

GM_wait();

对于其他问题,还有一些 $ 冲突...但您仍然希望安全在此加载订单。这是一个更便宜且仍然安全的版本......嗯,有效:

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

function GM_wait() {
    if (typeof unsafeWindow.jQuery == 'undefined') {
        window.setTimeout(GM_wait, 100);
        return;
    }
    unsafeWindow.jQuery(function($) {
        var link = $('<a href="#"><em>(Too Stupid)</em></a>').click(removeQuestion);
        $('div.question-summary').each(function (index, questionSummaryDiv) {
            var id = getId(questionSummaryDiv.id);
            if (idList.indexOf(id) != -1) {
                $(questionSummaryDiv).hide();
            } else {
                $('div.started', questionSummaryDiv).append(link.clone(true));
            }
        });
    });
}
function getId (idString) {
    return idString.split('-')[2];
}
function removeQuestion () {
    var q = unsafeWindow.jQuery(this).closest("div.question-summary").hide(250);
    idList.push(getId(q.attr("id")));
    setTimeout(function() {
        GM_setValue('idList', idList.join(','));
    }, 0);
    return false;
}

That script attempts to include jQuery first thing:

(function()
{
    if (typeof unsafeWindow.jQuery == 'undefined')
    {
        var GM_Head = document.getElementsByTagName('head')[0] || document.documentElement, GM_JQ = document.createElement('script');

        GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js';
        GM_JQ.type = 'text/javascript';
        GM_JQ.async = true;

        GM_Head.insertBefore(GM_JQ, GM_Head.firstChild);
    }        
    GM_wait();
})();

This issue there is that jQuery is guaranteed to be loaded on Stack Overflow to begin with...if it's not present you have much bigger issues. That whole jQuery replacement shouldn't happen, as it's both impacting already registered plugins (nuking then) and using a newer version of jQuery that Stack Exchange currently does, meaning other potentially breaking changes as well.

Since the script needs none of the latest functionality, that entire chunk above should simply be:

GM_wait();

For the other issues, there are a few more $ conflicts...but you still want to be safe with respect to load order here. Here's a cheaper and still safe version that...well, works:

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

function GM_wait() {
    if (typeof unsafeWindow.jQuery == 'undefined') {
        window.setTimeout(GM_wait, 100);
        return;
    }
    unsafeWindow.jQuery(function($) {
        var link = $('<a href="#"><em>(Too Stupid)</em></a>').click(removeQuestion);
        $('div.question-summary').each(function (index, questionSummaryDiv) {
            var id = getId(questionSummaryDiv.id);
            if (idList.indexOf(id) != -1) {
                $(questionSummaryDiv).hide();
            } else {
                $('div.started', questionSummaryDiv).append(link.clone(true));
            }
        });
    });
}
function getId (idString) {
    return idString.split('-')[2];
}
function removeQuestion () {
    var q = unsafeWindow.jQuery(this).closest("div.question-summary").hide(250);
    idList.push(getId(q.attr("id")));
    setTimeout(function() {
        GM_setValue('idList', idList.join(','));
    }, 0);
    return false;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文