6

I frequent a forum that has a horrible way of ignoring users. If you place someone on ignore, it almost makes that users presence more prevalent.

So I wrote this to hide them completely:

// ==UserScript==
// @name         Freddie
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  hide annoying forum users
// @author       You
// @match        http://www.scout.com/college/kansas/forums/*
// @grant        none
// ==/UserScript==
/* jshint -W097 */

'use strict';

function checkForDiv() { // crappy workaround function to wait for AJAX content      
    if (!document.getElementById("wrapper")) {        
        setTimeout(checkForDiv, 300);   
    } else {       
        checkNames();   
    }
}

function checkNames() {  
    var mybannedList = ["Pyros", "GOHawksGators", "th30r3o"]; // add usernames  here
    var nms = document.body.querySelectorAll('a.authName'), i = 0, len =  nms.length;
    for (i; i < len; i++) {
        if (mybannedList.indexOf(nms[i].innerHTML) != -1) {
          nms[i].parentNode.parentNode.style.display = "none";
        }
    }
}

checkForDiv();  

But when you go to the page with the ignored users they still appear, upon refreshing, the script runs and they disappear.

Please good sirs, what do I do?

Rodmentou
  • 1,610
  • 3
  • 21
  • 39
CodeMonkey
  • 61
  • 1
  • 3

1 Answers1

4

The site uses AJAX for navigation so the page address changes without reloading, that's why Tampermonkey doesn't inject your script when you navigate from another page on that site.

The simplest solution would be to include the entire site: // @match http://www.scout.com/*

There are other more advanced methods of detecting page transitions based on MutationObserver or some DOM event or property change that occurs on navigation.

Also beware of @grant none with jQuery loaded via @require: it breaks sites that also load jQuery unless you use jQuery.noConflict. The simplest solution is to remove that line as you don't need to access the web page variables.

P.S. There's a known timer-based wrapper: waitForKeyElements.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • A many thanks but bizarrely it's still not working despite the fact that now Tampermonkey shows that the script is running before the refresh. I still have to refresh to hide the users however. Any other ideas? – CodeMonkey Dec 02 '15 at 22:24
  • The element(s) should be checked periodically in a `setInterval` callback because the overall wrapper exists on all pages. – wOxxOm Dec 02 '15 at 22:27
  • Sorry to be a bother, but I'm not getting it to work, any chance you could show me how it should look? Thanks again. – CodeMonkey Dec 03 '15 at 00:40
  • Simply use `waitForKeyElements`, the link has an example and the code. – wOxxOm Dec 03 '15 at 00:43
  • Yeah sorry, I implemented it via that example but can't get it to work properly. Don't mean to be a pain if it's too much but I think I'm just not seeing something here. – CodeMonkey Dec 03 '15 at 00:53
  • I can't test it because the site requires membership. You can google `waitForKeyElements hide elements` for the examples. – wOxxOm Dec 03 '15 at 01:05
  • Thanks a bunch. I'll try that. – CodeMonkey Dec 03 '15 at 01:06