Corrections to Auto-print Regex

Several months ago I published a regular expression that would strip auto-print links from webpages. Unfortunately, the regex doesn’t actually catch everything, so here’s a corrected version:

replace(/((window|self)\.print(\(\))?|print\(\));?/ig, "");

This will catch all the following:

print()
print();
self.print
self.print()
self.print();
window.print
window.print()
window.print();

Further, the addition of the g after the / means all instances of the print command will be replaced, rather than just the first, and the i means this will work regardless of whether or not the command is written entirely in lowercase text.

I’ve also noticed a separate issue that I’m still not sure how to resolve — if the rule is set to run on all content-types, it will blindly strip out instances regardless of whether they’re actually operational JavaScript (e.g. the above ‘all the following’ section); if it is set to only run on the JavaScript content-type it will miss any instances that don’t appear in a JavaScript file.

I suppose an even more complicated variation could be made that checks for things like javascript: or <script>…</script>, but it really begins to spiral out of control. If anybody has a solution for this problem, I’d love to know.

written 27 November, 02009 Comments

Quick Tip: Stop Auto-printing with GlimmerBlocker

As part of my high-volume web reading habits, I click through to the print versions of articles whenever possible. Annoyingly, sites will often use JavaScript to automatically make the page print. Here’s a simple way to stop it.

  1. Download and install GlimmerBlocker.
  2. Create a new Filter group (top left of preference pane).
  3. Add a new Rule.
  4. Set the Action to “Whitelist URL, optionally modifying content”.
  5. Set Host: to “all hosts”.
  6. Go to the transform tab.
  7. Paste in the following text: replace(/(window|self)\.print(\(\))?;/, "");
  8. Save the rule.

The regex here will look for window.print or self.print (with or without parentheses) and strip it, thereby stopping the automatic print command.

I forget exactly how I came up with this, but it works great.

UPDATE: an improved version is available.

written 13 August, 02009 Comments