I have a search server that provides a test page where I can input a query and it returns the results in XML. I want to be able to go through the results in a more user frie
That script is nuking or adding to document.head
. You want to replace the whole document with transformed content. You could do that by changing location.href
to an appropriately constructed data:
URL. But a neater approach is to replace the whole document.documentElement
.
This script works on your test/sample XML file:
// ==UserScript==
// @name _Test XML Renderer
// @description stylesheet for xml results
// @include http://YOUR_SERVER.COM/YOUR_PATH/*.xml
// @grant none
// ==/UserScript==
var xsl_str = '\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
id \n\
category ID \n\
name \n\
phone \n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
\n\
';
var processor = new XSLTProcessor ();
var dataXSL = new DOMParser ().parseFromString (xsl_str, "text/xml");
processor.importStylesheet (dataXSL);
var newDoc = processor.transformToDocument (document);
//-- These next lines swap the new, processed doc in for the old one...
window.content = newDoc;
document.replaceChild (
document.importNode (newDoc.documentElement, true),
document.documentElement
);