4 >The Filter File</TITLE
7 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
10 TITLE="Privoxy 3.0.1 User Manual"
11 HREF="index.html"><LINK
14 HREF="actions-file.html"><LINK
17 HREF="templates.html"><LINK
20 HREF="../p_doc.css"></HEAD
31 SUMMARY="Header navigation table"
40 >Privoxy 3.0.1 User Manual</TH
48 HREF="actions-file.html"
78 >9. The Filter File</H1
80 > All text substitutions that can be invoked through the
84 HREF="actions-file.html#FILTER"
88 must first be defined in the filter file, which is typically
93 selected through the <TT
96 HREF="config.html#FILTERFILE"
102 > Typical reasons for doing such substitutions are to eliminate
103 common annoyances in HTML and JavaScript, such as pop-up windows,
104 exit consoles, crippled windows without navigation tools, the
105 infamous <BLINK> tag etc, to suppress images with certain
106 width and height attributes (standard banner sizes or web-bugs),
107 or just to have fun. The possibilities are endless.</P
109 > Filtering works on any text-based document type, including plain
110 text, HTML, JavaScript, CSS etc. (all <TT
114 MIME types). Substitutions are made at the source level, so if
117 >"roll your own"</SPAN
118 > filters, you should be
119 familiar with HTML syntax.</P
122 HREF="actions-file.html"
125 filter file is organized in sections, which are called <SPAN
132 here. Each filter consists of a heading line, that starts with the
149 >, and a short (one line)
156 > of what it does. Below that line
163 >, i.e. lines that define the actual
164 text substitutions. By convention, the name of a filter
165 should describe what the filter <SPAN
172 comment is used in the <A
173 HREF="http://config.privoxy.org/"
179 > Once a filter called <TT
185 in the filter file, it can be invoked by using an action of the form
189 HREF="actions-file.html#FILTER"
199 HREF="actions-file.html"
203 > A filter header line for a filter called <SPAN
217 >FILTER: foo Replace all "foo" with "bar"</PRE
223 > Below that line, and up to the next header line, come the jobs that
224 define what text replacements the filter executes. They are specified
225 in a syntax that imitates <A
226 HREF="http://www.perl.org/"
233 > operator. If you are familiar with Perl, you
234 will find this to be quite intuitive, and may want to look at the
236 HREF="http://www.oesterhelt.org/pcrs/pcrs.3.html"
240 for the subtle differences to Perl behaviour. Most notably, the non-standard
244 > is supported, which turns the default
245 to ungreedy matching.</P
247 > If you are new to regular expressions, you might want to take a look at
249 HREF="appendix.html#REGEX"
250 >Appendix on regular expressions</A
253 HREF="http://perldoc.com/perl5.6.1/pod/perl.html"
259 HREF="http://perldoc.com/perl5.6.1/pod/perlop.html#s-PATTERN-REPLACEMENT-egimosx"
265 > operator's syntax</A
267 HREF="http://perldoc.com/perl5.6.1/pod/perlre.html"
272 The below examples might also help to get you started.</P
280 >9.1. Filter File Tutorial</H2
282 > Now, let's complete our <SPAN
285 > filter. We have already defined
286 the heading, but the jobs are still missing. Since all it does is to replace
293 >, there is only one (trivial) job
310 > But wait! Didn't the comment say that <SPAN
320 > should be replaced? Our current job will only take
321 care of the first <SPAN
324 > on each page. For global substitution,
325 we'll need to add the <TT
344 > Our complete filter now looks like this:</P
354 >FILTER: foo Replace all "foo" with "bar"
361 > Let's look at some real filters for more interesting examples. Here you see
362 a filter that protects against some common annoyances that arise from JavaScript
363 abuse. Let's look at its jobs one after the other:</P
373 >FILTER: js-annoyances Get rid of particularly annoying JavaScript abuse
375 # Get rid of JavaScript referrer tracking. Test page: http://www.randomoddness.com/untitled.htm
377 s|(<script.*)document\.referrer(.*</script>)|$1"Not Your Business!"$2|Usg</PRE
383 > Following the header line and a comment, you see the job. Note that it uses
387 > as the delimiter instead of <TT
391 the pattern contains a forward slash, which would otherwise have to be escaped
397 > Now, let's examine the pattern: it starts with the text <TT
401 enclosed in parentheses. Since the dot matches any character, and <TT
407 >"Match an arbitrary number of the element left of myself"</SPAN
419 it matches the whole page, from the start of the first <script> tag.</P
421 > That's more than we want, but the pattern continues: <TT
423 >document\.referrer</TT
425 matches only the exact string <SPAN
427 >"document.referrer"</SPAN
435 >, i.e. preceded by a backslash, to take away its
436 special meaning as a joker, and make it just a regular dot. So far, the meaning is:
437 Match from the start of the first <script> tag in a the page, up to, and including,
440 >"document.referrer"</SPAN
448 in the page (and appear in that order).</P
450 > But there's still more pattern to go. The next element, again enclosed in parentheses,
453 >.*</script></TT
454 >. You already know what <TT
458 means, so the whole pattern translates to: Match from the start of the first <script>
459 tag in a page to the end of the last <script> tag, provided that the text
462 >"document.referrer"</SPAN
463 > appears somewhere in between.</P
465 > This is still not the whole story, since we have ignored the options and the parentheses:
466 The portions of the page matched by sub-patterns that are enclosed in parentheses, will be
467 remembered and be available through the variables <TT
471 the substitute. The <TT
474 > option switches to ungreedy matching, which means
478 > in the pattern will only <SPAN
482 text in between <SPAN
494 >"document.referrer"</SPAN
495 >, and that the second <TT
499 only span the text up to the <SPAN
507 >"</script>"</SPAN
509 tag. Furthermore, the <TT
512 > option says that the match may span
513 multiple lines in the page, and the <TT
516 > option again means that the
517 substitution is global.</P
519 > So, to summarize, the pattern means: Match all scripts that contain the text
522 >"document.referrer"</SPAN
523 >. Remember the parts of the script from
524 (and including) the start tag up to (and excluding) the string
527 >"document.referrer"</SPAN
531 >, and the part following
532 that string, up to and including the closing tag, as <TT
537 > Now the pattern is deciphered, but wasn't this about substituting things? So
538 lets look at the substitute: <TT
540 >$1"Not Your Business!"$2</TT
542 easy to read: The text remembered as <TT
548 >"Not Your Business!"</TT
556 the quotation marks!), followed by the text remembered as <TT
560 This produces an exact copy of the original string, with the middle part
563 >"document.referrer"</SPAN
570 > The whole job now reads: Replace <SPAN
572 >"document.referrer"</SPAN
576 >"Not Your Business!"</TT
577 > wherever it appears inside a
578 <script> tag. Note that this job won't break JavaScript syntax,
579 since both the original and the replacement are syntactically valid
580 string objects. The script just won't have access to the referrer
581 information anymore.</P
583 > We'll show you two other jobs from the JavaScript taming department, but
584 this time only point out the constructs of special interest:</P
594 ># The status bar is for displaying link targets, not pointless blahblah
596 s/window\.status\s*=\s*(['"]).*?\1/dUmMy=1/ig</PRE
605 > stands for whitespace characters (space, tab, newline,
606 carriage return, form feed), so that <TT
612 or more whitespace"</SPAN
620 makes this matching of arbitrary text ungreedy. (Note that the <TT
624 option is not set). The <TT
627 > construct means: <SPAN
636 > a double quote"</SPAN
641 a backreference to the first parenthesis just like <TT
645 with the difference that in the <SPAN
651 >, a backslash indicates
652 a backreference, whereas in the <SPAN
658 >, it's the dollar.</P
660 > So what does this job do? It replaces assignments of single- or double-quoted
663 >"window.status"</SPAN
664 > object with a dummy assignment
665 (using a variable name that is hopefully odd enough not to conflict with
666 real variables in scripts). Thus, it catches many cases where e.g. pointless
667 descriptions are displayed in the status bar instead of the link target when
668 you move your mouse over links.</P
678 ># Kill OnUnload popups. Yummy. Test: http://www.zdnet.com/zdsubs/yahoo/tree/yfs.html
680 s/(<body [^>]*)onunload(.*>)/$1never$2/iU</PRE
688 HREF="http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html#Events-eventgroupings-htmlevents"
692 > in the HTML DOM was a <SPAN
699 When I close a browser window, I want it to close and die. Basta.
700 This job replaces the <SPAN
706 >"<body>"</SPAN
707 > tags with the dummy word <TT
714 > option makes the pattern matching
715 case-insensitive. Also note that ungreedy matching alone doesn't always guarantee
716 a minimal match: In the first parenthesis, we had to use <TT
723 > to prevent the match from exceeding the
724 <body> tag if it doesn't contain <SPAN
730 > The last example is from the fun department:</P
740 >FILTER: fun Fun text replacements
742 # Spice the daily news:
744 s/microsoft(?!\.com)/MicroSuck/ig</PRE
753 > part (a so-called negative lookahead)
754 in the job's pattern, which means: Don't match, if the string
758 > appears directly following <SPAN
762 in the page. This prevents links to microsoft.com from being trashed, while
763 still replacing the word everywhere else.</P
773 ># Buzzword Bingo (example for extended regex syntax)
775 s* industry[ -]leading \
777 | customer[ -]focused \
779 | award[ -]winning # Comments are OK, too! \
780 | high[ -]performance \
781 | solutions[ -]based \
785 *<font color="red"><b>BINGO!</b></font> \
795 > option in this job turns on extended syntax, and allows for
796 e.g. the liberal use of (non-interpreted!) whitespace for nicer formatting. </P
798 > You get the idea?</P
806 SUMMARY="Footer navigation table"
817 HREF="actions-file.html"
835 HREF="templates.html"