javascript - Greasetampermonkey script not working unless page refreshed - Stack Overflow

I frequent a forum that has a horrible way of ignoring users.If you place someone on ignore, it almos

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 pletely:

// ==UserScript==
// @name         Freddie
// @namespace    /
// @version      0.1
// @description  hide annoying forum users
// @author       You
// @match        /*
// @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?

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 pletely:

// ==UserScript==
// @name         Freddie
// @namespace    http://tampermonkey/
// @version      0.1
// @description  hide annoying forum users
// @author       You
// @match        http://www.scout./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?

Share Improve this question edited Dec 2, 2015 at 21:48 Rodmentou 1,6403 gold badges22 silver badges40 bronze badges asked Dec 2, 2015 at 21:02 CodeMonkeyCodeMonkey 811 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

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./*

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745345450a4623522.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信