| 1 |
function insertAfter(newNode, referenceNode) { |
| 2 |
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); |
| 3 |
} |
| 4 |
function getElementByXPath(xPath, doc){ |
| 5 |
if(!doc) doc = document; |
| 6 |
if(doc.evaluate) return doc.evaluate(xPath, document, null, 9, null).singleNodeValue; |
| 7 |
// for IE |
| 8 |
while(xPath.charAt(0) == '/') xPath = xPath.substr(1); |
| 9 |
var prevElem = doc; |
| 10 |
var arr = xPath.split('/'); |
| 11 |
for(var i=0; i<arr.length; i++){ |
| 12 |
var step = arr[i].split(/(\w*)\[(\d*)\]/gi).filter(function(v){ return !(v==''||v.match(/\s/gi)) },this); |
| 13 |
var elem = step[0]; |
| 14 |
var elemNum = step[1]?step[1]-1:0; // -1 since xpath is 1 based |
| 15 |
if(i<arr.length-1) prevElem = prevElem.getElementsByTagName(elem)[elemNum]; |
| 16 |
else return prevElem.getElementsByTagName(elem)[elemNum]; |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Copyright (c) Mozilla Foundation http://www.mozilla.org/ |
| 22 |
* This code is available under the terms of the MIT License |
| 23 |
*/ |
| 24 |
if (!Array.prototype.filter) { |
| 25 |
Array.prototype.filter = function(fun /*, thisp*/) { |
| 26 |
var len = this.length >>> 0; |
| 27 |
if (typeof fun != "function") { |
| 28 |
throw new TypeError(); |
| 29 |
} |
| 30 |
|
| 31 |
var res = []; |
| 32 |
var thisp = arguments[1]; |
| 33 |
for (var i = 0; i < len; i++) { |
| 34 |
if (i in this) { |
| 35 |
var val = this[i]; // in case fun mutates this |
| 36 |
if (fun.call(thisp, val, i, this)) { |
| 37 |
res.push(val); |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return res; |
| 43 |
}; |
| 44 |
} |
| 45 |
/* END MIT License code */ |
| 46 |
|
| 47 |
var c = document.createElement("span"); |
| 48 |
var s = document.createElement("script"); |
| 49 |
c.innerHTML = '{{HTML}}'; |
| 50 |
s.innerHTML = "{{SCRIPT}}"; |
| 51 |
|
| 52 |
var n = getElementByXPath('{{XPATH}}'); |
| 53 |
|
| 54 |
// Default placement in case the xpath location is not found |
| 55 |
if (n==null){ |
| 56 |
n = document.getElementById("tbdefault"); |
| 57 |
} |
| 58 |
insertAfter(c,n); |
| 59 |
insertAfter(s,c); |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|