Greasemonkey 脚本隐藏 stackoverflow 帖子?
我发现自己一遍又一遍地阅读相同的问题,所以我想要一种隐藏问题的方法。
我有一个脚本可以完成应该做的事情,但是它削弱了现有的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果没有必要,切勿注入 JS,并且切勿在 FF GM 中使用页面的 jQuery —— 这是这种情况下错误的主要来源。
整个脚本应该是:
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:
该脚本首先尝试包含 jQuery:
这个问题是 jQuery 肯定会首先加载到 Stack Overflow 上...如果它不存在,那么您就会遇到更大的问题。整个 jQuery 替换不应该发生,因为它既影响已经注册的插件(然后是核武器),又使用 Stack Exchange 目前使用的更新版本的 jQuery,这也意味着其他潜在的重大变化。
由于该脚本不需要任何最新功能,因此上面的整个块应该只是:
对于其他问题,还有一些
$
冲突...但您仍然希望安全在此加载订单。这是一个更便宜且仍然安全的版本......嗯,有效:That script attempts to include jQuery first thing:
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:
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: