| @@ -1,83 +1,80 @@ | ||
| 1 | -(function () { | |
| 2 | - 'use strict'; | |
| 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 | +} | |
| 3 | 19 | |
| 4 | - function insertAfter(newNode, referenceNode) { | |
| 5 | - const parent = referenceNode.parentNode; | |
| 6 | - if (parent.lastChild === referenceNode) { | |
| 7 | - parent.appendChild(newNode); | |
| 8 | - } else { | |
| 9 | - parent.insertBefore(newNode, referenceNode.nextSibling); | |
| 10 | - } | |
| 11 | - } | |
| 12 | 20 | |
| 13 | - function getElementByXPath(xPath, doc = document) { | |
| 14 | - if (doc.evaluate) { | |
| 15 | - return doc.evaluate( | |
| 16 | - xPath, | |
| 17 | - doc, | |
| 18 | - null, | |
| 19 | - 9, | |
| 20 | - null | |
| 21 | - ).singleNodeValue; | |
| 22 | - } | |
| 21 | +/** | |
| 22 | + * Copyright (c) Mozilla Foundation http://www.mozilla.org/ | |
| 23 | + * This code is available under the terms of the MIT License | |
| 24 | + */ | |
| 25 | +if (!Array.prototype.filter) { | |
| 26 | + Array.prototype.filter = function(fun /*, thisp*/) { | |
| 27 | + var len = this.length >>> 0; | |
| 28 | + if (typeof fun != "function") { | |
| 29 | + throw new TypeError(); | |
| 30 | + } | |
| 23 | 31 | |
| 24 | - xPath = xPath.replace(/^\/+/, ''); | |
| 25 | - const steps = xPath.split('/'); | |
| 26 | - let current = doc; | |
| 32 | + var res = []; | |
| 33 | + var thisp = arguments[1]; | |
| 34 | + for (var i = 0; i < len; i++) { | |
| 35 | + if (i in this) { | |
| 36 | + var val = this[i]; // in case fun mutates this | |
| 37 | + if (fun.call(thisp, val, i, this)) { | |
| 38 | + res.push(val); | |
| 39 | + } | |
| 40 | + } | |
| 41 | + } | |
| 27 | 42 | |
| 28 | - for (let i = 0, l = steps.length; i < l && current; i += 1) { | |
| 29 | - const match = /([^\[\]]+)(?:\[(\d+)\])?/.exec(steps[i]); | |
| 30 | - if (!match) return null; | |
| 31 | - const [, tag, idx] = match; | |
| 32 | - const pos = idx ? (idx - 1) : 0; | |
| 33 | - current = current.getElementsByTagName(tag)[pos] || null; | |
| 43 | + return res; | |
| 44 | + }; | |
| 45 | +} | |
| 46 | +/* END MIT License code */ | |
| 47 | + | |
| 48 | +function injectWidgetByXpath(xpath){ | |
| 49 | + | |
| 50 | + var n = getElementByXPath(xpath); | |
| 51 | + | |
| 52 | + // Default placement in case the xpath location is not found | |
| 53 | + if (n==null){ | |
| 54 | + n = document.getElementById("tbdefault"); | |
| 34 | 55 | } |
| 35 | - return current; | |
| 36 | - } | |
| 56 | + innerInject(n); | |
| 57 | +} | |
| 37 | 58 | |
| 38 | - if (!Array.prototype.filter) { | |
| 39 | - Array.prototype.filter = function (callback, thisArg) { | |
| 40 | - if (typeof callback !== 'function') throw new TypeError(); | |
| 41 | - const out = []; | |
| 42 | - for (let i = 0, l = this.length >>> 0; i < l; i += 1) { | |
| 43 | - if (i in this) { | |
| 44 | - const val = this[i]; | |
| 45 | - if (callback.call(thisArg, val, i, this)) out.push(val); | |
| 46 | - } | |
| 47 | - } | |
| 48 | - return out; | |
| 49 | - }; | |
| 50 | - } | |
| 59 | +function injectWidgetByMarker(markerId){ | |
| 60 | + var markerNode = document.getElementById(markerId); | |
| 61 | + innerInject(markerNode.parentNode); | |
| 62 | +} | |
| 51 | 63 | |
| 52 | - function injectWidgetByXpath(xpath) { | |
| 53 | - const anchor = | |
| 54 | - getElementByXPath(xpath) || document.getElementById('tbdefault'); | |
| 55 | - if (anchor) innerInject(anchor); | |
| 56 | - } | |
| 64 | +function innerInject(node){ | |
| 57 | 65 | |
| 58 | - function injectWidgetByMarker(markerId) { | |
| 59 | - const markerNode = document.getElementById(markerId); | |
| 60 | - if (markerNode && markerNode.parentNode) innerInject(markerNode.parentNode); | |
| 61 | - } | |
| 66 | + var c = document.createElement("span"); | |
| 67 | + var s = document.createElement("script"); | |
| 62 | 68 | |
| 63 | - function innerInject(node) { | |
| 64 | - if (!node) return; | |
| 69 | + // Notice that minifier will replce single quotes to double quotes. here it must remain with single quotes | |
| 70 | + var noticeForDeveloper = "if JS crashes here, the first innerHTML value should be enclosed with single quotes instead of double, go to the minified version and change it"; | |
| 71 | + c.innerHTML = '{{HTML}}'; | |
| 72 | + s.innerHTML = "{{SCRIPT}}"; | |
| 65 | 73 | |
| 66 | - const fragment = document.createDocumentFragment(); | |
| 67 | - const container = document.createElement('span'); | |
| 68 | - const script = document.createElement('script'); | |
| 74 | + insertAfter(c,node); | |
| 75 | + insertAfter(s,c); | |
| 76 | +} | |
| 69 | 77 | |
| 70 | - container.insertAdjacentHTML('beforeend', '{{HTML}}'); | |
| 71 | - script.text = "{{SCRIPT}}"; | |
| 72 | 78 | |
| 73 | - fragment.appendChild(container); | |
| 74 | - fragment.appendChild(script); | |
| 75 | - insertAfter(fragment, node); | |
| 76 | - } | |
| 77 | 79 | |
| 78 | - window.insertAfter = insertAfter; | |
| 79 | - window.getElementByXPath = getElementByXPath; | |
| 80 | - window.injectWidgetByXpath = injectWidgetByXpath; | |
| 81 | - window.injectWidgetByMarker = injectWidgetByMarker; | |
| 82 | - window.innerInject = innerInject; | |
| 83 | -}()); | |
| 80 | + | |