Hack 97. Check Whether Pages Really Validate

 < Day Day Up > 

If someone puts a badge on her site claiming to be "Valid XHTML," run it through the W3C Validator and see if she's telling the truth.

You've probably seen them on personal weblogs or wikis. Stuffed down at the bottom of the page, amidst the copyright notices and privacy policies, a cluster of badges proudly proclaims, "This site is valid XHTML!" "This site is valid CSS!" "This site validates against some arcane standard you've never heard of!" But do they really validate? Let's find out.

12.4.1. The Code

This user script runs on all pages. It takes advantage of a feature of the W3C HTML Validator, which suggests that people put a link on their sites that points to http://validator.w3.org/check/referer. That URL uses the HTTP Referer header to automatically check the page you came from. If the page uses valid markup, the validator service will proclaim "This Page Is Valid (X)HTML"; otherwise, it will list all the errors on the page.

If the script finds such a link, it uses Greasemonkey's GM_xmlhttpRequest function to call the W3C validation service in the background and get the results. If it turns out that the page is not really valid, we replace the original link with a new link that reads "Invalid markup!"

Save the following script as reallyvalid.user.js:

 // ==UserScript== // @name Really Valid? // @namespace http://diveintomark.org/projects/greasemonkey/ // @description check if pages claiming to be valid (X)HTML really are // @include * // ==/UserScript== var snapValidLinks = document.evaluate( "//a[@href='http://validator.w3.org/check/referer']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); if (!snapValidLinks.snapshotLength) return; GM_xmlhttpRequest({ method: 'GET', url: 'http://validator.w3.org/check?uri=' + escape(location), onload: function(oResponse) { if (/This Page Is Valid/.test(oResponse.responseText)) return; for (var i = 0; i < snapValidLinks.snapshotLength; i++) { var elmInvalid = snapValidLinks.snapshotItem(i); elmInvalid.title = 'This page claimed to validate, but it lied'; elmInvalid.innerHTML = 'Invalid markup!'; } } }) 

12.4.2. Running the Hack

Before installing the user script, go to http://www.matrix.msu.edu and look in the pane on the right for the "W3C XHTML 1.0" badge. Then, install the user script from Tools Install This User Script, and refresh the MATRIX home page. After a second or two, the "W3C XHTML 1.0 badge will be replaced by a link that reads "Invalid markup!," as shown in Figure 12-5. Clicking the link confirms that there are numerous markup errors on the page.

Figure 12-5. Showing that MATRIX doesn't use valid XHTML 1.0, though they claim to


     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net