PluginProbe
Taboola / trunk
Taboola vtrunk
1.0.4 1.0.5 1.0.6 1.0.8 2.0.1 2.0.2 2.1.0 2.1.1 2.2.2 2.2.3 3.0.0 3.0.1 3.0.2 3.1.0 trunk 1.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.2 1.0.3
taboola / js / js_inject.js

js_inject.js in Taboola trunk, at js/js_inject.js

84 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function () {
2 'use strict';
3
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
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 }
23
24 xPath = xPath.replace(/^\/+/, '');
25 const steps = xPath.split('/');
26 let current = doc;
27
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;
34 }
35 return current;
36 }
37
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 }
51
52 function injectWidgetByXpath(xpath) {
53 const anchor =
54 getElementByXPath(xpath) || document.getElementById('tbdefault');
55 if (anchor) innerInject(anchor);
56 }
57
58 function injectWidgetByMarker(markerId) {
59 const markerNode = document.getElementById(markerId);
60 if (markerNode && markerNode.parentNode) innerInject(markerNode.parentNode);
61 }
62
63 function innerInject(node) {
64 if (!node) return;
65
66 const fragment = document.createDocumentFragment();
67 const container = document.createElement('span');
68 const script = document.createElement('script');
69
70 container.insertAdjacentHTML('beforeend', '{{HTML}}');
71 script.text = "{{SCRIPT}}";
72
73 fragment.appendChild(container);
74 fragment.appendChild(script);
75 insertAfter(fragment, node);
76 }
77
78 window.insertAfter = insertAfter;
79 window.getElementByXPath = getElementByXPath;
80 window.injectWidgetByXpath = injectWidgetByXpath;
81 window.injectWidgetByMarker = injectWidgetByMarker;
82 window.innerInject = innerInject;
83 }());
84