Javascript string decoding logic translated to Python - javascript

The following Javascript code will decode string 91ebf9e9f7a8a2a1a5d1a0a3a7bff2fefc
into an intelligible value:
!function() {
"use strict";
function e(e) {
try {
if ("undefined" == typeof console)
return;
"error"in console ? console.error(e) : console.log(e)
} catch (e) {}
}
function t(e) {
return d.innerHTML = '',
d.childNodes[0].getAttribute("href") || ""
}
function r(e, t) {
var r = e.substr(t, 2);
return parseInt(r, 16)
}
function n(n, c) {
for (var o = "", a = r(n, c), i = c + 2; i < n.length; i += 2) {
var l = r(n, i) ^ a;
o += String.fromCharCode(l)
}
try {
o = decodeURIComponent(escape(o))
} catch (u) {
e(u)
}
return t(o)
}
function c(t) {
for (var r = t.querySelectorAll("a"), c = 0; c < r.length; c++)
try {
var o = r[c]
, a = o.href.indexOf(l);
a > -1 && (o.href = "mailto:" + n(o.href, a + l.length))
} catch (i) {
e(i)
}
}
function o(t) {
for (var r = t.querySelectorAll(u), c = 0; c < r.length; c++)
try {
var o = r[c]
, a = o.parentNode
, i = o.getAttribute(f);
if (i) {
var l = n(i, 0)
, d = document.createTextNode(l);
a.replaceChild(d, o)
}
} catch (h) {
e(h)
}
}
function a(t) {
for (var r = t.querySelectorAll("template"), n = 0; n < r.length; n++)
try {
i(r[n].content)
} catch (c) {
e(c)
}
}
function i(t) {
try {
c(t),
o(t),
a(t)
} catch (r) {
e(r)
}
}
var l = "/cdn-cgi/l/email-protection#"
, u = ".__cf_email__"
, f = "data-cfemail"
, d = document.createElement("div");
i(document),
function() {
var e = document.currentScript || document.scripts[document.scripts.length - 1];
e.parentNode.removeChild(e)
}()
}();
Can anyone explain the actual logic of it, to be able to write it into Python? Or come up with an JS2PY solution of running the code against python-selected values?

You can use js2py module to rewrite automatically the Js code to Python:
import js2py
js_script = """\
function decode(email) {
function r(e, t) {
var r = e.substr(t, 2);
return parseInt(r, 16);
}
function n(n, c) {
for (var o = "", a = r(n, c), i = c + 2; i < n.length; i += 2) {
var l = r(n, i) ^ a;
o += String.fromCharCode(l);
}
return o;
}
var l = "/cdn-cgi/l/email-protection#";
return n(email, email.indexOf(l) + l.length);
}
"""
decoder = js2py.eval_js(js_script)
email = decoder(
"https://journals.sagepub.com/cdn-cgi/l/email-protection#7c0614041a454f4c483c4d4e4a521f1311431f1f414d454c454c494b4a4d3c0d0d521f1311"
)
print(email)
Prints your email.

Related

Is there a method to change the responseType to arraybuffer in XMLHttpRequest when async:false and using node.js

I'm scraping images from a comic site and it uses an encryption algorithm to retrieve the images and parse it to a blob link. I want to stop at the decryption and return the Uint8Array and use python code to write it to a file.
import execjs
def rfc(nam):
with open(nam, 'r') as f:
return f.read()
def decryptedData(lnk):
js = execjs.compile(rfc('getAndDecrypt.js'))
resp = js.call('i', lnk)
return bytes(list(resp.values()))
res = decryptedData('https://img.mwimser2.cc/static/upload/book/id/18928/547567/4c8ca89af48c44d45893f959b16447b2.webp?v=20220724')
with open('a.webp', 'wb') as f:
f.write(res)
// getAndDecrypt.js: get and decrypt image
let CryptoJS = require("crypto-js")
let jsdom = require("jsdom")
let { JSDOM } = jsdom
let dom = new JSDOM(`<!DOCTYPE html><html><body></body></html>`)
window = dom.window
document = window.document
XMLHttpRequest = window.XMLHttpRequest
var r = "my2ecret782ecret"
function i(e) {
let a = new XMLHttpRequest;
a.open("GET", e, false)
a.responseType = "arraybuffer" //this line gets changed
a.send()
return d(s2b(a.response), r)
}
function o(e) {
let t = e.sigBytes,
r = e.words,
a = new Uint8Array(t);
for (var n = 0, s = 0; n != t;) {
var i = r[s++];
if (a[n++] = (4278190080 & i) >>> 24, n == t) break;
if (a[n++] = (16711680 & i) >>> 16, n == t) break;
if (a[n++] = (65280 & i) >>> 8, n == t) break;
a[n++] = 255 & i
}
return a
}
function d(r, a) {
let s = r,
i = CryptoJS.enc.Utf8.parse(a),
l = CryptoJS.lib.WordArray.create(s),
f = o(CryptoJS.AES.decrypt({
ciphertext: l
}, i, {
iv: i,
padding: CryptoJS.pad.Pkcs7
}));
return f;
}
The link is here if you want to further investigate it.
$(function () {
webpMachine.webpSupport.then(e => {
var t = !1,
r = "my";
let a, n, s = "ret";
async function i(e, t) {
let a = new XMLHttpRequest;
a.open("GET", e, !0), a.responseType = "arraybuffer", a.onload = async function () {
4 == a.readyState && (200 == a.status ? t.src = await d(a.response, r, t) : (console.log(key, e), f(e, key)))
}, a.send()
}
function o(e) {
const t = e.sigBytes,
r = e.words,
a = new Uint8Array(t);
for (var n = 0, s = 0; n != t;) {
var i = r[s++];
if (a[n++] = (4278190080 & i) >>> 24, n == t) break;
if (a[n++] = (16711680 & i) >>> 16, n == t) break;
if (a[n++] = (65280 & i) >>> 8, n == t) break;
a[n++] = 255 & i
}
return a
}
r += "2ecret", r += "782ec", r += "ret", $("img.lazy_img").each(function (e) {
$(this).attr("data-original", "/static/images/imagecover.png"), $(this).on("load", function () {
($(this).attr("src").indexOf("blob:") > -1 || $(this).attr("src").indexOf("base64") > -1) && ($(this).css("width", "auto").css("display", "flex").css("min-height", "0px"), window.URL.revokeObjectURL($(this).attr("src")))
})
}), $("img.lazy_img").lazyload({
threshold: 1500,
effect: "fadeIn",
load: function (e) {
let t = $(this)[0],
r = t.getAttribute("src");
r.indexOf("blob:") < 0 && r.indexOf("base64") && i(t.getAttribute("data-r-src"), t)
}
});
const c = e => new Promise(t => setTimeout(t, e)),
l = e => {
let t = new FileReader;
return t.readAsDataURL(e), new Promise(e => {
t.onloadend = (() => {
e(t.result)
})
})
};
async function d(r, a, n) {
let s = r,
i = CryptoJS.enc.Utf8.parse(a),
l = CryptoJS.lib.WordArray.create(s),
d, f = o(CryptoJS.AES.decrypt({
ciphertext: l
}, i, {
iv: i,
padding: CryptoJS.pad.Pkcs7
})),
p = "";
if (e) {
let e = new Blob([f]);
p = URL.createObjectURL(e)
} else {
let e = 0;
for (;
"" === p && e < 100;) {
if (e++, !t) {
let e;
t = !0, p = await webpMachine.decode(f), t = !1
}
"" === p && await c(300)
}
"" == p && console.log($(n).attr("data-sort"), "fail!!")
}
return p
}
function f(e, t) {
let a = new XMLHttpRequest;
a.open("GET", e, !0), a.responseType = "arraybuffer", a.onload = function () {
200 == a.status && (t.src = d(a.response, r))
}, a.send()
}
})
});
This is the original code. Take a look at the XHR requests for the webp files.
nodejs version: v18.3.0

Cannot read property 'innerHTML' of undefined

I'm trying to test an app in salesforce lightning component, but when I load the app (I try this app) I got the error
Cannot read property 'innerHTML' of undefined
The associated code is here :
!(function(t, e) {
var r = function(e) {
(t.execScript || function(e) {
t["eval"].call(t, e)
})(e)
},
i = function(t, e) {
return t instanceof(e || Array)
},
s = document,
n = "getElementsByTagName",
a = "length",
c = "readyState",
l = "onreadystatechange",
u = s[n]("script"),
o = u[u[a] - 1],
f = o.innerHTML.replace(/^\s+|\s+$/g, "");
if (!t.ljs) {
var h = o.src.match(/checkLoaded/) ? 1 : 0,
d = s[n]("head")[0] || s.documentElement,
p = function(t) {
var e = {};
e.u = t.replace(/#(=)?([^#]*)?/g, function(t, r, i) {
e[r ? "f" : "i"] = i;
return ""
});
return e
},
v = function(t, e, r) {
var i = s.createElement(t),
n;
if (r) {
if (i[c]) {
i[l] = function() {
if (i[c] === "loaded" || i[c] === "complete") {
i[l] = null;
r()
}
}
} else {
i.onload = r
}
}
for (n in e) {
e[n] && (i[n] = e[n])
}
d.appendChild(i)
},
m = function(t, e) {
if (this.aliases && this.aliases[t]) {
var r = this.aliases[t].slice(0);
i(r) || (r = [r]);
e && r.push(e);
return this.load.apply(this, r)
}
if (i(t)) {
for (var s = t[a]; s--;) {
this.load(t[s])
}
e && t.push(e);
return this.load.apply(this, t)
}
if (t.match(/\.js\b/) || t.match(/\.sfjs\b/)) {
t = t.replace(".sfjs", "");
return this.loadjs(t, e)
} else if (t.match(/\.css\b/) || t.match(/\.sfcss\b/)) {
t = t.replace(".sfcss", "");
return this.loadcss(t, e)
} else {
return this.loadjs(t, e)
}
},
y = {},
g = {
aliases: {},
loadjs: function(t, r) {
var i = p(t);
t = i.u;
if (y[t] === true) {
r && r();
return this
} else if (y[t] !== e) {
if (r) {
y[t] = function(t, e) {
return function() {
t && t();
e && e()
}
}(y[t], r)
}
return this
}
y[t] = function(e) {
return function() {
y[t] = true;
e && e()
}
}(r);
r = function() {
y[t]()
};
v("script", {
type: "text/javascript",
src: t,
id: i.i,
onerror: function(t) {
if (i.f) {
var e = t.currentTarget;
e.parentNode.removeChild(e);
v("script", {
type: "text/javascript",
src: i.f,
id: i.i
}, r)
}
}
}, r);
return this
},
loadcss: function(t, e) {
var r = p(t);
t = r.u;
y[t] || v("link", {
type: "text/css",
rel: "stylesheet",
href: t,
id: r.i
});
y[t] = true;
e && e();
return this
},
load: function() {
var t = arguments,
r = t[a];
if (r === 1 && i(t[0], Function)) {
t[0]();
return this
}
m.call(this, t[0], r <= 1 ? e : function() {
g.load.apply(g, [].slice.call(t, 1))
});
return this
},
addAliases: function(t) {
for (var e in t) {
this.aliases[e] = i(t[e]) ? t[e].slice(0) : t[e]
}
return this
}
};
if (h) {
var j, b, x, A;
for (j = 0, b = u[a]; j < b; j++) {
(A = u[j].getAttribute("src")) && (y[A.replace(/#.*$/, "")] = true)
}
x = s[n]("link");
for (j = 0, b = x[a]; j < b; j++) {
(x[j].rel === "stylesheet" || x[j].type === "text/css") && (y[x[j].getAttribute("href").replace(/#.*$/, "")] = true)
}
}
t.ljs = g
}
f && r(f)
})(window);
But I'm a novice with javascript and I don't know how to fix it (i'd like to test this app)
Thanks !
That just means that the element that you are looking for does not exist:
var elementNotFound = document.getElementById("noSuchId"); // Does not exist
console.log("The element:", elementNotFound);
console.log(elementNotFound.innerHTML);
As you have posted the mimified JavaScript and no HTML, I don't know how to help further then saying: Make use the ID you are using to find the element is correct and has no spelling mistakes, and that the element is actually in the HTML.
Hope this helps.

Refresh Code when Infinite Scroll Is Called

How to I call a JavaScript function when Tumblr's infinite scroll loads more posts?
I figure it would have to be some sort of listener function for when tumblrAutoPager.init us called. I found the infinite scroll code online and don't really understand it.
var tumblrAutoPager = {
url: "http://proto.jp/",
ver: "0.1.7",
rF: true,
gP: {},
pp: null,
ppId: "",
LN: location.hostname,
init: function () {
if ($("autopagerize_icon") || navigator.userAgent.indexOf('iPhone') != -1) return;
var tAP = tumblrAutoPager;
var p = 1;
var lh = location.href;
var lhp = lh.lastIndexOf("/page/");
var lht = lh.lastIndexOf("/tagged/");
if (lhp != -1) {
p = parseInt(lh.slice(lhp + 6));
tAP.LN = lh.slice(7, lhp);
} else if (lht != -1) {
tAP.LN = lh.slice(7);
if (tAP.LN.slice(tAP.LN.length - 1) == "/") tAP.LN = tAP.LN.slice(0, tAP.LN.length - 1);
} else if ("http://" + tAP.LN + "/" != lh) {
return;
};
var gPFncs = [];
gPFncs[0] = function (aE) {
var r = [];
for (var i = 0, l = aE.length; i < l; i++) {
if (aE[i].className == "autopagerize_page_element") {
r = gCE(aE[i]);
break;
}
}
return r;
};
gPFncs[1] = function (aE) {
var r = [];
for (var i = 0, l = aE.length; i < l; i++) {
var arr = aE[i].className ? aE[i].className.split(" ") : null;
if (arr) {
for (var j = 0; j < arr.length; j++) {
arr[j] == "post" ? r.push(aE[i]) : null;
}
}
}
return r;
};
gPFncs[2] = function (aE) {
var r = [];
var tmpId = tAP.ppId ? [tAP.ppId] : ["posts", "main", "container", "content", "apDiv2", "wrapper", "projects"];
for (var i = 0, l = aE.length; i < l; i++) {
for (var j = 0; j < tmpId.length; j++) {
if (aE[i].id == tmpId[j]) {
r = gCE(aE[i]);
tAP.ppId = aE[i].id;
break;
}
}
}
return r;
};
for (var i = 0; i < gPFncs.length; i++) {
var getElems = gPFncs[i](document.body.getElementsByTagName('*'));
if (getElems.length) {
tAP.gP = gPFncs[i];
tAP.pp = getElems[0].parentNode;
break;
}
}
function gCE(pElem) {
var r = [];
for (var i = 0, l = pElem.childNodes.length; i < l; i++) {
r.push(pElem.childNodes.item(i))
}
return r;
}
if (!tAP.pp) {
return;
}
sendRequest.README = {
license: 'Public Domain',
url: 'http://jsgt.org/lib/ajax/ref.htm',
version: 0.516,
author: 'Toshiro Takahashi'
};
function chkAjaBrowser() {
var A, B = navigator.userAgent;
this.bw = {
safari: ((A = B.split('AppleWebKit/')[1]) ? A.split('(')[0].split('.')[0] : 0) >= 124,
konqueror: ((A = B.split('Konqueror/')[1]) ? A.split(';')[0] : 0) >= 3.3,
mozes: ((A = B.split('Gecko/')[1]) ? A.split(' ')[0] : 0) >= 20011128,
opera: ( !! window.opera) && ((typeof XMLHttpRequest) == 'function'),
msie: ( !! window.ActiveXObject) ? ( !! createHttpRequest()) : false
};
return (this.bw.safari || this.bw.konqueror || this.bw.mozes || this.bw.opera || this.bw.msie)
}
function createHttpRequest() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest()
} else {
if (window.ActiveXObject) {
try {
return new ActiveXObject('Msxml2.XMLHTTP')
} catch (B) {
try {
return new ActiveXObject('Microsoft.XMLHTTP')
} catch (A) {
return null
}
}
} else {
return null
}
}
};
function sendRequest(E, R, C, D, F, G, S, A) {
var Q = C.toUpperCase() == 'GET',
H = createHttpRequest();
if (H == null) {
return null
}
if ((G) ? G : false) {
D += ((D.indexOf('?') == -1) ? '?' : '&') + 't=' + (new Date()).getTime()
}
var P = new chkAjaBrowser(),
L = P.bw.opera,
I = P.bw.safari,
N = P.bw.konqueror,
M = P.bw.mozes;
if (typeof E == 'object') {
var J = E.onload;
var O = E.onbeforsetheader
} else {
var J = E;
var O = null
}
if (L || I || M) {
H.onload = function () {
J(H);
H.abort()
}
} else {
H.onreadystatechange = function () {
if (H.readyState == 4) {
J(H);
H.abort()
}
}
}
R = K(R, D);
if (Q) {
D += ((D.indexOf('?') == -1) ? '?' : (R == '') ? '' : '&') + R
}
H.open(C, D, F, S, A);
if ( !! O) {
O(H)
}
B(H);
H.send(R);
function B(T) {
if (!L || typeof T.setRequestHeader == 'function') {
T.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
}
return T
}
function K(X, V) {
var Z = [];
if (typeof X == 'object') {
for (var W in X) {
Y(W, X[W])
}
} else {
if (typeof X == 'string') {
if (X == '') {
return ''
}
if (X.charAt(0) == '&') {
X = X.substring(1, X.length)
}
var T = X.split('&');
for (var W = 0; W < T.length; W++) {
var U = T[W].split('=');
Y(U[0], U[1])
}
}
}
function Y(b, a) {
Z.push(encodeURIComponent(b) + '=' + encodeURIComponent(a))
}
return Z.join('&')
}
return H
}
function addNextPage(oj) {
if (oj.status == 404) {
tAP.remainFlg = false;
return;
}
var d = document.createElement("div");
d.innerHTML = oj.responseText;
var posts = tAP.gP(d.getElementsByTagName("*"));
if (posts.length < 2) {
tAP.rF = false;
return;
}
d = document.createElement("div");
d.className = "tumblrAutoPager_page_info";
tAP.pp.appendChild(d);
for (var i = 0; i < posts.length; i++) {
tAP.pp.appendChild(posts[i]);
}
var footer = $("footer");
footer ? footer.parentNode.appendChild(footer) : null;
tAP.rF = true;
}
watch_scroll();
function watch_scroll() {
var d = document.compatMode == "BackCompat" ? document.body : document.documentElement;
var r = d.scrollHeight - d.clientHeight - (d.scrollTop || document.body.scrollTop);
if (r < d.clientHeight * 2 && tAP.rF) {
tAP.rF = false;
p++;
sendRequest(addNextPage, "", "GET", "http://" + tAP.LN + "/page/" + p, true);
}
setTimeout(arguments.callee, 200);
};
function $(id) {
return document.getElementById(id)
};
},
switchAutoPage: function () {
this.rF = !this.rF;
var aE = document.getElementsByTagName('*');
for (var i = 0, l = aE.length; i < l; i++) {
if (aE[i].className == "tAP_switch") {
aE[i].firstChild.nodeValue = this.rF ? "AutoPage[OFF]" : "AutoPage[ON]";
}
}
}
};
window.addEventListener ? window.addEventListener('load', tumblrAutoPager.init, false) : window.attachEvent ? window.attachEvent("onload", tumblrAutoPager.init) : window.onload = tumblrAutoPager.init;

Function Conflict with Jquery.min.js

currently working on metal website so i need unit conversion tool like in this site
http://www.theunitconverter.com/unit-conversion-widget.html
i try add the code, but its not working.
and its working when i remove this
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
the problem is i need those min.js script for another jquery function.
and i try to add the javascript converter to another file but i couldn't place the correct noconflict function. the js goes like this.
function IsNum(a) {
if (a != null && a != "") {
return !isNaN(a)
}
return false
}
function $(b) {
if (typeof b == "string") {
return document.getElementById(b)
}
return b
}
function v(g) {
var f = $("a").value;
var e = $("b").value;
var j = $("c").value;
var h = $("d").value;
if (g == 0) {
if (!IsNum(f)) {
return false
}
$("c").value = $("a").value * h / e
} else {
if (!IsNum(j)) {
return false
}
$("a").value = $("c").value * e / h
}
}
function o(a) {
var j = $("e").value;
var d = $("f").value;
var c = $("g").value;
var b = $("h").value;
if (a == 0) {
if (!IsNum(j)) {
return false
}
$("g").value = $("e").value * b / d
} else {
if (!IsNum(c)) {
return false
}
$("e").value = $("g").value * d / b
}
}
function r(x) {
var q;
var p;
var ub;
var ud;
var s;
if (x == 1) {
ub = $("b");
ud = $("d");
ua = $("a");
uc = $("c");
s = $("u").value
} else {
ub = $("f");
ud = $("h");
ua = $("e");
uc = $("g");
s = $("s").value
}
var f = eval(s + "f");
var t = eval(s + "t");
var u0 = eval(s + "0");
var u1 = eval(s + "1");
var amount = eval(u0)[t] / eval(u0)[f];
var z = 0;
for (var n in u) {
if (s == u[n]) {
z = 1
}
}
ua.value = "1";
uc.value = amount;
ub.options.length = 0;
ud.options.length = 0;
if (z == 1) {
var o = new Option("Common units", "0");
o.disabled = "false";
var r = new Option("Common units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u0) {
p = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
o = new Option("Other units", "0");
o.disabled = "false";
r = new Option("Other units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u1) {
p = new Option(n, u1[n]);
q = new Option(n, u1[n]);
ub.add(p);
ud.add(q)
}
} else {
for (var n in u0) {
p = new Option(n, u0[n]);
q = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
}
};
Thanks for any kind of help.
Wrap everything inside
(function($) {
...
})(jQuery);
Your code should be
(function($) {
function IsNum(a) {
if (a != null && a != "") {
return !isNaN(a)
}
return false
}
function $(b) {
if (typeof b == "string") {
return document.getElementById(b)
}
return b
}
function v(g) {
var f = $("a").value;
var e = $("b").value;
var j = $("c").value;
var h = $("d").value;
if (g == 0) {
if (!IsNum(f)) {
return false
}
$("c").value = $("a").value * h / e
} else {
if (!IsNum(j)) {
return false
}
$("a").value = $("c").value * e / h
}
}
function o(a) {
var j = $("e").value;
var d = $("f").value;
var c = $("g").value;
var b = $("h").value;
if (a == 0) {
if (!IsNum(j)) {
return false
}
$("g").value = $("e").value * b / d
} else {
if (!IsNum(c)) {
return false
}
$("e").value = $("g").value * d / b
}
}
function r(x) {
var q;
var p;
var ub;
var ud;
var s;
if (x == 1) {
ub = $("b");
ud = $("d");
ua = $("a");
uc = $("c");
s = $("u").value
} else {
ub = $("f");
ud = $("h");
ua = $("e");
uc = $("g");
s = $("s").value
}
var f = eval(s + "f");
var t = eval(s + "t");
var u0 = eval(s + "0");
var u1 = eval(s + "1");
var amount = eval(u0)[t] / eval(u0)[f];
var z = 0;
for (var n in u) {
if (s == u[n]) {
z = 1
}
}
ua.value = "1";
uc.value = amount;
ub.options.length = 0;
ud.options.length = 0;
if (z == 1) {
var o = new Option("Common units", "0");
o.disabled = "false";
var r = new Option("Common units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u0) {
p = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
o = new Option("Other units", "0");
o.disabled = "false";
r = new Option("Other units", "0");
r.disabled = "false";
ub.add(o);
ud.add(r);
for (var n in u1) {
p = new Option(n, u1[n]);
q = new Option(n, u1[n]);
ub.add(p);
ud.add(q)
}
} else {
for (var n in u0) {
p = new Option(n, u0[n]);
q = new Option(n, u0[n]);
if (n == f) {
p.selected = "true"
}
q = new Option(n, u0[n]);
if (n == t) {
q.selected = "true"
}
ub.add(p);
ud.add(q)
}
}
};
})(jQuery);

Javascript error: Cannot read property 'parentNode' of null

The javascript I am using:
javascript: c = '{unit}, ({coords}) {player} |{distance}| {return}';
p = ['Scout', 'LC', 'HC', 'Axe', 'Sword', 'Ram', '***Noble***'];
function V() {
return 1;
}
window.onerror = V;
function Z() {
d = (window.frames.length > 0) ? window.main.document : document;
aid = d.getElementById('editInput').parentNode.innerHTML.match(/id\=(\d+)/)[1];
function J(e) {
vv = e.match(/\d+\|\d+/g);
return (vv ? vv[vv.length - 1].match(/((\d+)\|(\d+))/) : null);
}
function K(e) {
f = parseInt(e, 10);
return (f > 9 ? f : '0' + f);
}
function L(g, e) {
return g.getElementsByTagName(e);
}
function N(g) {
return g.innerHTML;
}
function M(g) {
return N(L(g, 'a')[0]);
}
function O() {
return k.insertRow(E++);
}
function W(f) {
return B.insertCell(f);
}
function P(g, e) {
g.innerHTML = e;
return g;
}
function X(e) {
C = B.appendChild(d.createElement('th'));
return P(C, e);
}
function Y(f) {
return K(f / U) + ':' + K(f % (U) / T) + ':' + K(f % T);
}
U = 3600;
T = 60;
R = 'table';
S = 'width';
s = L(document, R);
for (j = 0; j < s.length; j++) {
s[j].removeAttribute(S);
if (s[j].className == 'main') {
s = L(L(s[j], 'tbody')[0], R);
break;
}
}
D = 0;
for (j = 0; j < s.length; j++) {
s[j].removeAttribute(S);
if (s[j].className = 'vis') {
k = s[j];
if (t = k.rows) {
D = t.length;
break;
}
}
}
for (E = 0; E < D; E++) {
l = t[E];
m = (u = l.cells) ? u.length : 0;
if (m) {
u[m - 1].colSpan = 5 - m;
if (N(u[0]) == 'Arrival:') {
Q = new Date(N(u[1]).replace(/<.*/i, ''));
} else {
if (N(u[0]) == 'Arrival in:') {
v = N(u[1]).match(/\d+/ig);
}
}
if (E == 1) {
G = M(u[2]);
}
if (E == 2) {
w = J(M(u[1]));
}
if (E == 4) {
x = J(M(u[1]));
}
}
}
y = v[0] * U + v[1] * T + v[2] * 1;
n = w[2] - x[2];
o = w[3] - x[3];
F = Math.sqrt(n * n + o * o);
H = F.toFixed(2);
E = D - 2;
s = L(k, 'input');
i = s[1];
h = s[0];
h.size = T;
B = O();
P(W(0), 'Distance:').colSpan = 2;
P(W(1), H + ' Fields').colSpan = 2;
B = O();
X('Unit');
X('Sent');
X('Duration');
X('Name to');
c = c.replace(/\{coords\}/i, w[1]).replace(/\{distance\}/i, H).replace(/\{player\}/i, G);
for (j in p) {
z = Math.round([9.00000000, 10.00000000, 11.00000000, 18.0000000015, 22.00000000, 30.00000000, 35.0000000][j] * T * F);
A = z - y;
if (A > 0) {
I = Y(z);
B = O();
P(W(0), p[j]);
P(W(1), A < T && 'just now' || A < U && Math.floor(A / T) + ' mins ago' || Y(A) + ' ago');
P(W(2), I);
C = W(3);
q = C.appendChild(i.cloneNode(1));
r = C.appendChild(h.cloneNode(1));
r.id = 'I' + j;
r.value = c.replace(/\{duration\}/i, I).replace(/\{sent\}/i, new Date(Q.valueOf() - z * 1000).toLocaleString().replace(/.\d{4}/i, '').replace(/(\w{3})\w*/i, '$1')).replace(/\{return\}/i, new Date(Q.valueOf() + z * 1000).toString().replace(/\w+\s*/i, '').replace(/(\d*:\d*:\d*)(.*)/i, '$1')).replace(/\{unit\}/i, p[j]).replace(/\{attack_id\}/i, aid);
q.onmousedown = new Function('h.value=d.getElementById(\'I' + j + '\').value;');
}
}
}
Z();
The error I receive:
Uncaught TypeError: Cannot read property 'parentNode' of null
A URL looks like this:
game.php?village=2100&id=4348754&type=other&screen=info_command
There are two possibilities:
editInput is a typo, and the actual id of that element is different (ids are case-sensitive).
You are executing this code while the DOM is not ready. To prevent this, execute the code just before the </body> closing tag, or wrap it in an event handler for the load event of window or the DOMContentLoaded event of document.
EDITED How to wrap your code:
window.onload = function() {
//your code here
};
In my case it was a conflict between slider and form validator as they were located on different pages but javascript file would of target both of them simultaneously, so all pages that had no slider would come up with "Cannot read property 'parentNode' of null" error, so I have added to the slider If statement:
if(document.getElementById("mainImage")){
var myImage = document.getElementById("mainImage");
var linkElement = myImage.parentNode;
that solved my problem, try it
I was trying to remove child element from parent:
constructor(private renderer: Renderer2) { }
removeChildEl() {
this.renderer.removeChild(parent, child);
}
but for some reason parent-element does not exist so I need to check via if condition:
removeChildEl() {
if(parent)
this.renderer.removeChild(parent, child);
}
so if the parent exists then remove the child otherwise do nothing.

Categories