Saturday, April 4, 2009

Salon Letter Filter Script

[WARNING: This script can no longer be used due to changes in Salon's Letter format. The new script is here.]
[On 11/10/09 Salon again changed the format of their letter pages. A new script to filter Salon letters is again available here. The old code is left here in case Salon changes the format back again.]
For those who have been unable to install the Salon Letter Filter Greasemonkey script from the link to the file, here is the text of the script. To install, copy everything between //BEGIN CODE and //END CODE into a text file (do not copy //BEGIN CODE and //END CODE). Name the text file salonletterfilter.user.js and save it. Open the file in your browser (you can drag and drop). If Greasemonkey is installed and active, when you open the file a Greasemonkey dialog will appear asking if you want to install the script. Click on install.

IMPORTANT: Your file name must end with .user.js for Greasemonkey to recognize it as a user script.

Do not panic if it appears that the lines of code end in midair on the blog page. It is all there even if it doesn't display because of the Blogger page layout. Simply select the code, copy it, and paste it into a new text file. Notepad or some other fairly simple text editor is recommended for this.
//BEGIN CODE
//
// MANUAL CONFIGURATION REQUIRED
//
// To configure the letters filter, you must have a "killfile" variable configured.
// A template for this variable is given below. The "killfile" varable includes a
// comma-delimited string of the nyms of the authors that you wish to filter out.
// A sample "killfile variable configuration looks like:
//
// new String(killfile = 'author1,author2,author3');
// or
// new String(killfile = 'author');
//
// Simply replace the word "authors" in the template below with the nyms of the authors you
// wish to expunge, each one separated by a comma, and save the file.
// Entries are case insensitive. Since the "killfile" will become a regular expression,
// regular expression metacharacters (such as "." or "+") appearing in a nym should be
// preceded by a backslash ("\"; e.g., 'L\.W\.M\.') in order to work properly.
//
new String(killfile = 'authors');
if (killfile.length == 0) exit;
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Letter_Filter", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name Salon Letter filter
// @author Frankly, my dear, ...
// @namespace http://language-grammar.blogspot.com/2009/04/filter-for-salon-letters.html
// @description Filter letters from Salon. WARNING: manual configuration required. Before use you must edit the script and enter your "killfile" in the string variable near the top of the script.
// @include http://letters.salon.com/*
// @version 0.0.1
// ==/UserScript==

//
// Set debug to 1 to see debug output in Javascript console
//
var debug = 1;


//
// Only print information to console if debug is on
//
function myGM_log(args) {
if (debug == 1) {
GM_log(args);
}
}


//
// Shortcut function to evalute an XPath in the document
//
function xpath(query, sourceDoc) {
return document.evaluate(query, sourceDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
}

myregexp = new RegExp(killfile.replace(/, */g, "|"), "i");
myGM_log(myregexp);
var allLets, thisLet, allAuthors, thisAuthor, allFooters, thisFooter;
allLets = xpath("//li[@class='letter_node']",document);
allAuthors = xpath("//div[@class='letter_entry_author'] | //div[@class='letter_entry_author premium']",document);
allFooters = xpath("//div[@class='letter_entry_footer']",document);
for (var i = 0; i < allLets.snapshotLength; i++) {
thisLet = allLets.snapshotItem(i);
thisAuthor = allAuthors.snapshotItem(i);
thisFooter = allFooters.snapshotItem(i);
var author = thisAuthor.innerHTML;
if (myregexp.test(author)) {
mymatch = myregexp.exec(author);
thisLet.innerHTML = "<h3><b>Letter from " + mymatch + " deleted</h3></b>" + thisFooter.innerHTML +"<hr>";
//thisLet.innerHTML = "";
}
}
//END CODE

Friday, April 3, 2009

A Filter for Salon Letters (Important Update below — 9/15/09) (Updated again — 11/10/09)

In view of the vitriolic and acerbic nature of the comment section of Glenn's recent postings at UT, many commenters have asked for a filter to remove certain authors' letters from the comment thread. It got so bad, that one commenter was ready to kill:
Baring that, I'd kill for a "ignore" or "filter" option. At least that way I can completely skip the crap and not waste the time and bytes downloading text I'm not going to read.
KB4Hire

At the time, I said that I was too busy to take the necessary steps to produce such a filter, but this is the kind of simple programming task that I just can't pass up. So I brushed up my javascript and read a book on Greasemonkey and then wasted a day of my life writing a Q&D® filter to remove letters from specified authors from comments pages. It turns out it was about as simple as I predicted. And while it doesn't save on downloading, because you have to download the letters before you check and eliminate them, it does mean that you don't have to read the vitriol and invective that some people seem to be incapable of excluding from their writing. This will be a boon to those with peptic ulcers and high blood pressure.

The file is salonletterfilter.user.js, and, if you already have the Greasemonkey add-on for Firefox installed and active, it can be installed simply by clicking on the file name and selecting "Install" when the Greasemonkey dialog panel appears. If you don't already have Greasemonkey, install it by clicking on the link. If you're using Internet Explorer, eat your heart out.

Note that there is some manual configuration necessary to make the script functional. After installing the script, open the Greasemonkey "Manage User Scripts" dialog either by right-clicking on the Greasemonkey icon in the status bar at the bottom of your browser window or from the browser's "Tools" menu. Once the dialog panel is open, select "Salon Letter Filter" and click on "Edit". This will open the script in your designated text editor. Then go to the first line of the script that is not a comment (i.e., does not begin with "//") and replace the word "authors" with your kill list. Use a comma to separate individual authors if you want to exclude more than one author. Save the file, and the next time you open a Salon letters page, letters from the authors listed in "killfile" will be deleted.

The configured version is fairly "polite" in that it leaves behind a notice that a letter from a particular author has been deleted, the permalink to the message (as an icon) and the datestamp. As a less polite option, there is also a more vicious version that removes letters completely. To implement this, edit the script and in the last line of the code (//thisLet.innerHTML = "";) delete the "//" at the beginning. Unwanted letters will disappear without a trace.

You can, of course, retrieve deleted letters by editing the "killfile" variable at any time. And there is no need to uninstall the script if you no longer wish to have a killfile. Simply set the variable "killfile" to an empty string ('') and the script will stop. The only thing to keep in mind is that the "killfile" variable is used to create a regular expression so regular expression metacharacters have to have a backslash ("\") in front of them for the script to work properly.

[Disclaimer: No warranty, expressed or implied, is associated with this script. The use of this script is entirely at the user's risk. This offer void where prohibited by law.]

UPDATE: While we were sleeping, Salon changed the format of their letters pages (obviously to make them less readable than they were before). This means that the old version of the Salon Letters Filter no longer works. I have revised the Greasemonkey script and it is still available through the link above in this posting. In order to install the new version you will have to first delete the old one. For those who are unable to access the script through the link, the text of the new script is now available.

LATER UPDATE: Salon has mercifully restored the previous letters settings. There is now no need for version 0.0.2 of the Salon Letter Filter and the original version works fine. That is all.

STILL LATER UPDATE: Salon has now changed the format of their letter pages in conformance with their new beta "business model". This change is likely to be permanent. The text of the new script for the beta layout is now available. The link to a downloadable version of the script no longer works.