Tuesday, September 15, 2009

Salon Letter Filter Script beta

[(11/10/09) Salon has again changed its Letter format in conformance with its new beta page layout. Presumably this change will be permanent]

Salon has changed the format of their letters pages, obviously in order to make them even less readable than they were before, and the earlier Salon Letter Filter Greasemonkey script no longer works. Here is the text of a revised script that should function until Salon changes the format again. 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 salonletterfilterbeta.user.js and save it. If you already have Salon Letter Filter installed, you do not need to uninstall it before installing the new file. Simply open the new 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.
Alternatively, if you think you know what you are doing, you can simply edit the installed script and replace those lines of code that have been changed with the new ones.

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.
This version also removes ads from Salon Letter pages. If you actually prefer to see the ads, simply remove the code at the end that begins with “// ad remover”.
There is also an enhancement that increases the size of the poster's name slightly to make it easier to see. If you prefer the original setting, edit the file and put “//” at the beginning of the line that begins “thisAuthor.innerHTML = "<span style=”.
The greasemonkey script for the letter filter is no longer available as a download. To install it, you must copy the code from here into a file on your own computer. We regret the inconvenience.

//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 beta
// @author Frankly, my dear, ...
// @namespace http://language-grammar.blogspot.com/2009/09/salon-letter-filter-script-version-002.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");
//alert(myregexp);
var allLets, thisLet, allAuthors, thisAuthor, allFooters, thisFooter, allHeads, thisHead;
allHeads = xpath("//div[@class='posts']/div[@class='letter']/h2[@class='headline md']",document);
allLets = xpath("//div[@class='posts']/div[@class='letter']/div[@class='deck md']",document);
allAuthors = xpath("//div[@class='posts']/div[@class='letter']/div[@class='byline'] | //div[@class='posts']/div[@class='letter']/div[@class='byline premium']",document);
allFooters = xpath("//div[@class='letter_entry_footer']",document);
for (var i = 0; i < allLets.snapshotLength; i++) {
thisHead = allHeads.snapshotItem(i);
thisLet = allLets.snapshotItem(i);
thisAuthor = allAuthors.snapshotItem(i);
thisFooter = allFooters.snapshotItem(i);
var author = thisAuthor.innerHTML;
thisAuthor.innerHTML = "<span style='font-size:90%;color:black'>" + thisAuthor.innerHTML + "</span>";
if (myregexp.test(author)) {
mymatch = myregexp.exec(author);
thisLet.innerHTML = "<h2><b>Letter from " + mymatch + " deleted</h2></b>" + thisFooter.innerHTML;
thisHead.innerHTML = "";
thisFooter.innerHTML = "";
thisAuthor.innerHTML = "";
//thisLet.innerHTML = "";
}
}
// ad remover
var allAds, thisAd;
allAds = xpath("//div[@class='ad_content' | //div[@id='ad_Top']",document);
for (var j = 0; j < allAds.snapshotLength; j++) {
thisAd = allAds.snapshotItem(j);
thisAd.innerHTML = "";
}
//END CODE

12 comments:

Karen M said...

Frankly, My Dear... you are indeed a gem!

Frankly, my dear, ... said...

Ain't it the trooth. However, I see that the previous format has been restored at Salon so the new version is not now needed. I'll keep it though, just in case.

LittleBrother said...

Bless your heart! How I utterly LOATHE SalonFugly!

However, the new beta script isn't working for me. I copied/pasted the script correctly, pasted in my fairly long string of 'author's from the previous script, added a backslash to Tim W\. Brown and Rob H\., and installed it successfully.

The script "looks" right, from a non-technical visual standpoint.

Greasemonkey is enabled; I've tried restarting Firefox and even rebooting, but it's not working.

Any suggestions? Thanks!

Frankly, my dear, ... said...

Try taking out the comment markers "//" at the beginning of the line that says "//alert(myregexp);". This should give you an alert window containing your killfile list as a regular expression when you reload the page. This will tell you 1) that the script is working 2) let you check the regular expression to make sure that there are no errors that prevent it from operating. If you aren't that familiar with regular expressions, you can post it here and I'll parse it for you.

LittleBrother said...

LittleBrother said...

Oh, well! Blogger wouldn't accept the rest of the script, even after I substituted square brackets for angle brackets in SPAN and H2 tags because Blogger rejected them.

It finally just ate the edited comment and gave me an error message.

Thanks for your help, even though at the moment I'm back to Square Minus One.

I think I properly added in extra scripts; I don't understand the language, but am bright enough to successfully tweak registries, install files, and such.

Since I suspect that most Salon regulars would prefer a script that will undo as many SalonFugly elements as possible, perhaps you can eventually produce a single script. Thanks again for your expertise and consideration.

Frankly, my dear, ... said...

Okay, I deleted your attempt to post the file as it wasn't particularly successful. However, I copied out your killfile string and pasted it into my script and it ran fine. There is a hard carraige return inside the string (after "Steve") but it is possible that that was introduced in copying and pasting.

One thing that I noted is that a single \ doesn't come through into the regular expression. Apparently JavaScript eats one \ when it creates the regular expression so to get a \ in the regular expression you have to put \\ in the string. This doesn't really make any difference with the . character since it is a metacharacter for any single character and will match a literal . as well.

There may be another debugging tool available to you. It depends on how you installed Firefox. Look under the "Tools" menu and see if there is an item "Error Console". If there is, you can open it and it will record any JavaScript errors that occur when you run the script. I believe the Error Console is only available if you installed Firefox with the web-developer option. But it's a simple matter to check to see if it's there or not. If it is, it is, if it's not, it's not.

I still recommend that you uncomment the "alert(myregexp);" line just to make sure that you get an alert message. That will at least let you know that the script is running and hasn't been aborted due to a syntax error. The "alert()" function is an important debugging tool in that it lets you check on the nature of your data flow at any particular point.

LittleBrother said...

I have an Error Console, but of course I can't read Error Creole. FWIW, here are all of the "errors" and a few representative "warnings". The "warnings" are too numerous to copy.

If anything leaps out at you-- either further corrective action or the conclusion that continued attempts at troubleshooting are a waste of time, please let me know.

Thanks again.

Error: OAS_AD is not defined
Source File: http://www.salon.com/news/opinion/glenn_greenwald/
Line: 283

Error: $("ad_messageboard_x10") is null
Source File: http://www.salon.com/news/opinion/glenn_greenwald/
Line: 91

Error: OAS_AD is not defined
Source File: http://letters.salon.com/opinion/greenwald/2009/11/11/iraq/view/?show=all
Line: 164

Warning: Unknown property 'zoom'. Declaration dropped.
Source File: http://www.salon.com/css/header_body_footer.css?20091028
Line: 451

Warning: Error in parsing value for 'filter'. Declaration dropped.
Source File: http://www.salon.com/css/topic_story.css?20091021
Line: 551

Warning: Unknown property '-moz-opacity'. Declaration dropped.
Source File: http://www.salon.com/news/opinion/glenn_greenwald/
Line: 0

Frankly, my dear, ... said...

Okay, none of these errors come from the letter filter. They are all generated by the Salon letter page. This is shown by the "Source File:" information. If there is an error in the script, you will get a "Source File:" that leads to wherever the .user.js file with the script is. For example, I commented out the killfile definition and ran the script. The error message was:

Error: killfile is not defined
Source File: file:///C:/Documents%20and%20Settings/Rhett.PRESARIO/Application%20Data/Mozilla/Firefox/Profiles/0027xuhn.Default%20User/gm_scripts/salon_letter_filter-2/salon_letter_filter.user.js
Line: 21

To see if the script is generating any errors, clear the error console, select "Errors" rather than "All", and then load a Salon letter page. If there are no errors and the page isn't modified by the script then it would seem that the script is not running at all. In that case, there may be a conflict with some other greasemonkey script or with some other FF add-on. If you have other greasemonkey scripts installed, try moving the Letter Filter to the top of the list so that it runs first. You can do this in the "Manage User Scripts" dialog box: Right-click on the greasemonkey icon in the browser status line and select "Manage User Scripts" from the menu. Instructions for re-ordering the list are at the bottom of the list of user scripts.

If that doesn't work, I don't know what to tell you without being able to see what happens when you try to run the script. If you don't get any joy from debugging, I'd suggest that you start all over from scratch. Copy the code into a new file, add the new code at the end of the file (being careful to paste the code from section 4) in front of the code from section 3)), save the new file as a .user.js file, uninstall the current file and install the new one. It may be easier if you do it incrementally. First take just the letter filter (killfile) and see if that works. If it does, edit the file and add additional code a section at a time until it stops working. Then you'll have an idea where the problem is.

Chuan Wen said...

Can you help me out with this? I have been reading and doing what you recommended to the previous poster to try to get the script to run.

My error console says there is an illegal character and when I click on the link it takes me to this line in my script, which happens to be the first line:

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}

Any idea how to fix this? It doesnt appear that the script is doing anything for me right now.

Thanks!

Frankly, my dear, ... said...

I believe I answered this at <http://letters.salon.com/opinion/greenwald/2009/11/17/iran/permalink/a8d60f8ea3103d49afb03680f94c54ee.html>

Robert said...

Thank you, that was extremely valuable and interesting...I will be back again to read more on this topic.