d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Helpful Greasemonkey User Script
Prev12345Next
Add Reply New Topic New Poll
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Oct 18 2009 08:28pm
Quote (llamaoo7 @ Sun, 18 Oct 2009, 21:26)
Hmm, I don't know if it was my browser but I had to make a couple of tweaks to get it to work.  I've only tried to pick up on web programming the past week or so.

Code
// ==UserScript==
// @name           Ignore Muted
// @namespace      http://asbands.d2jsp.org/
// @description    Ignores all posts made by Muted by making them invisible
// @include        http://forums.d2jsp.org/*
// ==/UserScript==

function takeoutPoster(posterName) {
  var posts = document.getElementsByTagName('fieldset').wrappedJSObject;
  for (var i = 0; i < posts.length; i++) {
      var post = posts.item(i);
      if (isPostedBy(post, posterName)) {
          post.style.visibility = 'collapse';
      }
  }
}

function isPostedBy(post, posterName) {
  var legend = post.firstChild;
  if (!legend) return false;
  else {
      var link = legend.firstChild;
      if (!link) return false;
      else if (link.innerHTML) {
          return (rtrim(link.text) == posterName);
      }
  }
}

function rtrim(stringToTrim) {
return stringToTrim.replace(/\s+$/,"");
}


takeoutPoster('Muted');


Testing testing, one two three! :D
Member
Posts: 6,953
Joined: Sep 27 2003
Gold: 518.50
Oct 18 2009 08:33pm
Quote (llamaoo7 @ Sun, Oct 18 2009, 09:26pm)
Hmm, I don't know if it was my browser but I had to make a couple of tweaks to get it to work.  I've only tried to pick up on web programming the past week or so.

Not a bad improvement. You gave me the idea to let people just define those they want to block by regular expressions:
Code
// ==UserScript==
// @name           Ignore Muted
// @namespace      http://asbands.d2jsp.org/
// @description    Ignores all posts made by Muted by making them invisible
// @include        http://forums.d2jsp.org/*
// ==/UserScript==
function takeoutPoster(posterName) {
   var posts = document.getElementsByTagName('fieldset').wrappedJSObject;
   for (var i = 0; i < posts.length; i++) {
       var post = posts.item(i);
       if (isPostedBy(post, posterName)) {
           post.style.visibility = 'collapse';
       }
   }
}

function isPostedBy(post, posterName) {
   var legend = post.firstChild;
   if (!legend) return false;
   else {
       var link = legend.firstChild;
       if (!link) return false;
       else if (link.innerHTML) {
           return (link.innerHTML.search(posterName) != -1);
       }
   }
}

takeoutPoster(/Muted/);

By changing the last line, you can regexp block whoever you want (easily)!
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Oct 18 2009 08:37pm
Wow. Peace at last. Thank you for this.
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Oct 18 2009 09:00pm
Quote (Minkomonster @ Sun, 18 Oct 2009, 21:37)
Wow. Peace at last. Thank you for this.


Doesn't work on Opera. :(
Member
Posts: 6,953
Joined: Sep 27 2003
Gold: 518.50
Oct 18 2009 10:17pm
Revision 2, which lets you re-show the posts which were hidden.

Code
// ==UserScript==
// @name           Ignore Muted
// @namespace      http://asbands.d2jsp.org/
// @description    Ignores all posts made by Muted by making them invisible
// @include        http://forums.d2jsp.org/*
// ==/UserScript==
var __num__ = 0;

function takeoutPoster(posterRegex) {
   var posts = document.getElementsByTagName('fieldset').wrappedJSObject;
   for (var i = 0; i < posts.length; i++) {
       var post = posts.item(i);
       if (isBadPoster(post, posterRegex)) {
           post.style.visibility = 'collapse';
           var div = document.createElement('div');
           post.id = 'post_' + __num__;
           div.id = 'div_' + __num__;
           var link = document.createElement('a');
           link.href = 'javascript:void(document.getElementById("' + post.id + '").style.visibility = "visible")';
           link.innerHTML = 'Show Hidden Post by ' + getPoster(post);
           div.appendChild(link);
           post.parentNode.insertBefore(div, post);
           __num__ = __num__ + 1;
       }
   }
}

function getPoster(post) {
   var legend = post.firstChild;
   if (!legend) return null;
   else {
       var link = legend.firstChild;
       if (!link) return null;
       else return link.innerHTML;
   }
}

function matches(name, regex) {
   return name.search(regex) != -1;
}

function isBadPoster(post, regex) {
   var name = getPoster(post);
   if (name) return matches(name, regex);
   else return false;
}

takeoutPoster(/Muted/);
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Oct 19 2009 12:17am
Quote (ASBands @ Sun, 18 Oct 2009, 23:17)
Revision 2, which lets you re-show the posts which were hidden.

Code// ==UserScript==
// @name          Ignore Muted
// @namespace     http://asbands.d2jsp.org/
// @description    Ignores all posts made by Muted by making them invisible
// @include       http://forums.d2jsp.org/*
// ==/UserScript==
var __num__ = 0;

function takeoutPoster(posterRegex) {
    var posts = document.getElementsByTagName('fieldset').wrappedJSObject;
    for (var i = 0; i < posts.length; i++) {
        var post = posts.item(i);
        if (isBadPoster(post, posterRegex)) {
            post.style.visibility = 'collapse';
            var div = document.createElement('div');
            post.id = 'post_' + __num__;
            div.id = 'div_' + __num__;
            var link = document.createElement('a');
            link.href = 'javascript:void(document.getElementById("' + post.id + '").style.visibility = "visible")';
            link.innerHTML = 'Show Hidden Post by ' + getPoster(post);
            div.appendChild(link);
            post.parentNode.insertBefore(div, post);
            __num__ = __num__ + 1;
        }
    }
}

function getPoster(post) {
    var legend = post.firstChild;
    if (!legend) return null;
    else {
        var link = legend.firstChild;
        if (!link) return null;
        else return link.innerHTML;
    }
}

function matches(name, regex) {
    return name.search(regex) != -1;
}

function isBadPoster(post, regex) {
    var name = getPoster(post);
    if (name) return matches(name, regex);
    else return false;
}

takeoutPoster(/Muted/);


Still doesn't work on Opera. :cry:

Quote
  var posts = document.getElementsByTagName('fieldset').wrappedJSObject;
  for (var i = 0; i < posts.length; i++) {


posts.length is undefined :(

This post was edited by Muted on Oct 19 2009 12:18am
Member
Posts: 31,680
Joined: Nov 10 2007
Gold: 1.00
Oct 19 2009 12:42am
Doesn't even work on Mozilla Firefox! LOL!
Member
Posts: 6,953
Joined: Sep 27 2003
Gold: 518.50
Oct 19 2009 01:55am
Revision 3!
This adds cool new features, like the ability to collapse and expand every post on the forum. It also automatically collapses posts that are more than six hours older than the last time you read the given topic.

Code
// ==UserScript==
// @name           D2jsp Forums Helper
// @namespace      http://asbands.d2jsp.org/
// @description    Adds random features to d2jsp forums browsing
// @include        http://forums.d2jsp.org/topic.php?*
// @include        http://forums.d2jsp.org/pm.php?*
// ==/UserScript==
var __num__ = 0;
var collapseOldPosts = false;

function rollPosts(posterRegex, cutoffDate) {
   var posts = document.getElementsByTagName('fieldset').wrappedJSObject;
   for (var i = 0; i < posts.length; i++) {
       var post = posts.item(i);
       var posterName = getPoster(post);
       if (posterName) {
           var div = document.createElement('div');
           post.id = 'post_' + __num__;
           div.id = 'div_' + __num__;
           var link = document.createElement('a');
           link.id = 'lnk_' + __num__;
           link.innerHTML = '+';
           div.appendChild(link);
           div.style.cssFloat = 'left';

           post.parentNode.insertBefore(div, post);
           if (isBadPoster(post, posterRegex)) collapsePost(post, link);
           else if (collapseOldPosts && isPostOlder(post, cutoffDate)) collapsePost(post, link);
           else expandPost(post, link);

           __num__ = __num__ + 1;
       }
   }
}

function getPoster(post) {
   var legend = post.firstChild;
   if (!legend) return null;
   else {
       var link = legend.firstChild;
       if (!link) return null;
       else return link.innerHTML;
   }
}

function getPostTime(post) {
   var infoDiv = post.childNodes[2];
   var dateString = infoDiv.childNodes[5].innerHTML;

   return parseDate(dateString);
}

function parseDate(dateString) {
   dateString = dateString.substring(dateString.indexOf(' ') + 1);
   //    extract month
   var monthString = dateString.substring(0, dateString.indexOf(' '));
   month = getMonth(monthString);
   dateString = dateString.substring(monthString.length + 1);
   //    extract day of month
   var day = dateString.substring(0, dateString.indexOf(' '));
   dateString = dateString.substring(day.length + 1);
   //    extract the year
   var commaIdx = dateString.indexOf(',');
   var spaceIdx = dateString.indexOf(' ');
   var year = dateString.substring(0, commaIdx);
   if (commaIdx + 1 != spaceIdx) {
       year = dateString.substring(0, spaceIdx);
       dateString = dateString.substring(year.length + 1);
   } else {
       dateString = dateString.substring(year.length + 2);
   }
   //    extract the hour and minute
   var hour = dateString.substring(0,2);
   var mins = dateString.substring(3,5);

   var d = new Date();
   d.setFullYear(year, month, day);
   d.setHours(hour);
   if (dateString.substring(5,6) == 'p') d.setHours(d.getHours() + 12);
   d.setMinutes(mins);
   return d;
}

function getMonth(m) {
   if (matches(m, /Jan/i)) { return 0; }
   if (matches(m, /Feb/i)) { return 1; }
   if (matches(m, /Mar/i)) { return 2; }
   if (matches(m, /Apr/i)) { return 3; }
   if (matches(m, /May/i)) { return 4; }
   if (matches(m, /Jun/i)) { return 5; }
   if (matches(m, /Jul/i)) { return 6; }
   if (matches(m, /Aug/i)) { return 7; }
   if (matches(m, /Sep/i)) { return 8; }
   if (matches(m, /Oct/i)) { return 9; }
   if (matches(m, /Nov/i)) { return 10; }
   if (matches(m, /Dec/i)) { return 11; }
}

function isPostOlder(post, cutoff) {
   var postTime = getPostTime(post) - 0;//    get it into long mode
   return postTime < cutoff;
}

function matches(name, regex) {
   return name.search(regex) != -1;
}

function isBadPoster(post, regex) {
   var name = getPoster(post);
   if (name) return matches(name, regex);
   else return false;
}

function togglePost(post, header) {
   if (post.style.visibility == 'collapse') expandPost(post, header);
   else collapsePost(post, header);
}

function collapsePost(post, header) {
   var posterName = getPoster(post);
   post.style.visibility = 'collapse';
   header.innerHTML = '+ ' + posterName + getPostTime(post);
}

function expandPost(post, header) {
   post.style.visibility = 'visible';
   header.innerHTML = '&mdash;';
}

//    adds the click listener, which allows us to intercept user clicks
document.addEventListener('click', function(event) {
       var target = event.target;
       //    is the user clicked on one of the created links
       if (matches(target.id, /^lnk_/)) {
           //    flip the post associated with the link
           var postId = 'post_' + target.id.substring(4);//4 = length of 'lnk_'
           var post = document.getElementById(postId).wrappedJSObject;
           togglePost(post, target);
       }
   }, true);

function getAutohideRegex() {
   var hideRegexString = GM_getValue('hideRegex');
   var regex = null;
   if (!hideRegexString) {
       hideRegexString = prompt('You have not yet specified a regular expression to automatically hide users.  Please input one now (or blank to not autohide).', 'Muted');
       if (hideRegexString.length > 0) {
           regex = new RegExp(hideRegexString);
           GM_setValue('hideRegex', hideRegexString);
       } else GM_setValue('@@NONE');
   } else if (hideRegexString != '@@NONE') {
       regex = new RegExp(hideRegexString);
   }
   return regex;
}

function resetAutohideRegex() {
   var regexString = prompt('Please set the regular expression used to select users to automatically hide (or blank not to hide any).','Muted');
   if (regexString.length > 0) GM_setValue('hideRegex', regexString);
   else GM_setValue('hideRegex', '@@NONE');
}

function onScriptLoad() {
   var purl = '' + window.location;
   purl = purl.substring(purl.indexOf('t=') + 2, purl.indexOf('f=') - 1);
   if (GM_registerMenuCommand) GM_registerMenuCommand('Set D2jsp Forums Autohide', resetAutohideRegex);
   var lastTime = GM_getValue('time_' + purl);
   if (lastTime) {
       lastTime = parseDate(lastTime);
       collapseOldPosts = true;
   }
   GM_setValue('time_' + purl, '' + new Date());

   var regex = getAutohideRegex();
   if (regex) rollPosts(regex, lastTime - 6*60*60*1000);//cutoff time is six hours later
}

onScriptLoad();


You can also set whatever string you want as the regular expression through a totally unintuitive menu command! See it in action!


Is anybody having problems getting it working on their Firefox?

This post was edited by ASBands on Oct 19 2009 02:10am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 19 2009 07:49am
thats hot...

can greasemonkey work on firefox 3.5.3? last time i checked i tihnk it only worked for 2,x,x series
Member
Posts: 6,953
Joined: Sep 27 2003
Gold: 518.50
Oct 19 2009 07:51am
Quote (AbDuCt @ Mon, Oct 19 2009, 08:49am)
thats hot...

can greasemonkey work on firefox 3.5.3? last time i checked i tihnk it only worked for 2,x,x series

I'm running it on my Firefox 3.0.14. FF 3.5 is still too unstable for me to move up to it. In either case, the Greasemonkey extension works on it.

Known issues
  • In threads where a lot of collapsing is going on, a huge blank white space appears below all the posts. This seems to stem from Gecko's size being determined by the static elements.
  • Testing in other versions of Firefox.


This post was edited by ASBands on Oct 19 2009 07:55am
Go Back To Programming & Development Topic List
Prev12345Next
Add Reply New Topic New Poll