为什么隐藏侧边栏的Greasemonkey JQuery脚本在Stack Exchange站点上工作但不在Stack Overflow上工作?

时间:2015-04-16 18:07:12

标签: javascript jquery greasemonkey

我被侧栏中的相关问题和热门网络问题分心,所以我写了一个Greasemonkey脚本来隐藏它们。该脚本适用于Stack Exchange站点,但不适用于Stack Overflow本身。

// ==UserScript==
// @name        minimal-stack-exchange
// @include     http://www.stackoverflow.com/*
// @include     http://www.stackexchange.com/*
// @include     http://*.stackexchange.com/*
// @include     http://*.stackoverflow.com/*
// @description Hide distracting links from StackExchange pages
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant       GM_addStyle
// ==/UserScript==

$("#sidebar").hide();
$("#herobox").hide();
$("#footer").hide();

我清除了我的Firefox缓存,使用Inspect Element确定我使用了正确的div ID,但没有成功。

2 个答案:

答案 0 :(得分:3)

解决方案是通配符http://stackoverflow.com未捕获http://*.stackoverflow.com/*,因此添加另一个包含行修复了问题。

// ==UserScript==
// @name        minimal-stack-exchange
// @include     http://www.stackoverflow.com/*
// @include     http://www.stackexchange.com/*
// @include     http://*.stackexchange.com/*
// @include     http://*.stackoverflow.com/*
// @include     http://stackexchange.com/*
// @include     http://stackoverflow.com/*
// @description Hide distracting links from StackExchange pages
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant       GM_addStyle
// ==/UserScript==


$("#sidebar").hide();
$("#herobox").hide();
$("#footer").hide();

答案 1 :(得分:3)

在这种情况下,更好的解决方案是使用@matchDoc代替@include@match智能处理领先的*.并提供更好的性能和安全性,尤其是在Chrome上。

以下@match指令完成:

  1. 匹配所有 Stack Exchange网站。 (问题@includes会错过一些重要的问题。)
  2. 匹配http://https://,因为所有Stack Exchange站点都支持SSL。
  3. // @match       *://*.askubuntu.com/*
    // @match       *://*.mathoverflow.net/*
    // @match       *://*.serverfault.com/*
    // @match       *://*.stackapps.com/*
    // @match       *://*.stackexchange.com/*
    // @match       *://*.stackoverflow.com/*
    // @match       *://*.superuser.com/*
    

    注意:对于实际脚本,我建议您也排除某些堆栈交换页面,除非您明确编写它们。

    请参阅 Stack Apps 上的"Complete list of sites to @include / @match into my script?"