What does the "+e+" mean in this Javascript? - javascript

Can someone give me a hand and tell me what does this "+e+" do in the following script (taken from
https://tracking.crealytics.com/lib/multi_conversion.min.js
)? I highlighted it in black:
(function(){var
t,e,n;this.__multi_conversion_tracking=function(e,n){var i,c,r;return
i=document.getElementsByTagName("body")[0],c=document.createElement("div"),c.id="multi_conversion_tracking",c.style.display="none",r=document.createElement("iframe"),r.src=t(e,n,1),c.appendChild(r),i.appendChild(c)},n=function(){return"https:"===location.protocol.toLowerCase()?"https":"http"},t=function(t,e,i){return
null==i&&(i=1),""+n()+"://tracking.crealytics.com/"+t+"/multi_check.php
?data="+e+" &random="+(new Date).getTime()+"
&frame="+i},e=function(t,e){return-1!==t.indexOf(e,t.length-e.length)}}).call(this);
I am trying to figure out why this script is not parsing correctly the following tag:
<script
src="https://tracking.crealytics.com/lib/multi_conversion.min.js"></script>
<script type="text/javascript"> var transactionString =
{{CrealyticsProductsInfo}};__multi_conversion_tracking(70,
"transactionString"); </script> <noscript> <div style="display:inline;"> <img
src="https://tracking.crealytics.com/70/multi_check.php?data=transactionString">
</div> </noscript>
this is the assignation I give the variable in my tracking code:
var divElement = document.createElement("Div");
divElement.id = "transactionString";
divElement.setAttribute('data-transaction-string', products_info);
It is supposed to mimic the following div element:
<div id='transactionString' data-transaction-string='DATA'></div>

multi_conversion_tracking function takes two parameters, e and n. the value of the first parameter (e) will be appended to the data parameter in that query string being composed using + e +

e it's just argument of function t. It concatenating e argument with another parts of url.
__multi_conversion_tracking call t function r.src = t(e, n, 1)
(function() {
var t, e, n;
this.__multi_conversion_tracking = function(e, n) {
var i, c, r;
return i = document.getElementsByTagName("body")[0],
c = document.createElement("div"),
c.id = "multi_conversion_tracking",
c.style.display = "none",
r = document.createElement("iframe"),
r.src = t(e, n, 1),
c.appendChild(r),
i.appendChild(c)
}, n = function() {
return "https:" === location.protocol.toLowerCase() ? "https" : "http"
}, t = function(t, e, i) {
return null == i && (i = 1), "" + n() + "://tracking.crealytics.com/" + t + "/multi_check.php ?data=" + e + " &random=" + (new Date).getTime() + " &frame=" + i
}, e = function(t, e) {
return -1 !== t.indexOf(e, t.length - e.length)
}
}).call(this)

You concat a string with a variable, or multiple variables, with this.
For example
var e = "johan855";
var string = "Hello " + e + ".";
console.log(string);
And / or
var e = "johan855";
var a = "Hello ";
var dot = ".";
var string = a + e + dot;
console.log(string);
will output
Hello johan855.

The "+e+" part in this javascript code is just a concatenation of the var e with other elements to create a string.

Related

Javascript regex in with special character in between

Hello can anyone help my about regex. this is the string
((11A1:I19 + 11A1:K19 + 11A1:L19 + 11A1:I20 + 11A1:K20) - (11A1:N19 + 11A1:N20))
and this is the regex
/([0-9a-z])\w+:\w+([0-9-a-z])/g
I want to take 11A1:I19, 11A1:K19, etc.. and replace it with values so the string will look like this (1767+154+1123 - (151-17)) This is the full code
$f.each(function() {
var formula = $(this).data("formula");
var formula = $f.data("formula");
formula.split(/([0-9a-z])\w+:\w+([0-9-a-z])/g)
.forEach(function(el) {
if (el) {
var hy = el.split(':');
let v = $('[data-sheet="' + hy[0] + '"][data-cell="' + hy[1] + '"]').val();
formula = formula.replace(el, v);
}
});
console.log(formula)
var result = eval(formula);
$f.val(result)
});
I believe you want to do something like this (not tested with jquery)
$f.each(function() {
var formula = $(this).data("formula");
var formula = $f.data("formula");
formula.split(/([0-9a-z]+:[0-9a-z]+)/gi)
.forEach(function(el) {
if (el) {
var hy = el.split(':');
if (hy.length==2) {
let v = $('[data-sheet="' + hy[0] + '"][data-cell="' + hy[1] + '"]').val();
formula = formula.replace(el, v);
}
}
});
console.log(formula)
var result = eval(formula);
$f.val(result)
});
Update: After some more thinking, this code is more compact and possibly easier to read:
$f.each(function() {
var formula = $(this).data("formula");
var formula = $f.data("formula");
var Re=/([0-9a-z]+):([0-9a-z]+)/gi;
var hy;
var replaced=formula;
while ((hy=Re.exec(formula))!=null) {
let v = $('[data-sheet="' + hy[1] + '"][data-cell="' + hy[2] + '"]').val();
replaced = replaced.replace(hy[0], v);
}
console.log(replaced)
var result = eval(replaced);
$f.val(result)
});
For safety reasons, I would also check that v is a valid number before replacing it in the formula. That will avoid evaluating some code that might be a valid javascript expression with dire consequences. You can test it with:
if (isNaN(v+0)) continue;
Add it before replacing hy[0] with v.

Array does not work in Javascript

I tried to make a function that returns an array, the output should be "My name is Sarah Adam" but actually it does not return anything
/*global s:true*/
var m = 'My name is ';
function updateMsg(h) {
"use strict";
var el = m + h;
s = ['Adam', 'Joseph'];
return s;
}
var n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah")[0];
you are returning s (the array) - I think you want to return the concatenated message. As in:
Updated to include variable last names
var m = 'My name is ';
function updateMsg(h, index) {
"use strict";
var el = m + h;
// array of last names
var s = ['Adam', 'Joseph'];
return el + ' ' + s[index]; // return the concatenated string instead
}
var n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah", 0); // invoke with param
// console log (confirmation)
console.log(updateMsg("Sarah", 0));
console.log(updateMsg("Meenah", 1));
<div id="msg">
hw
</div>
You could use currying to accomplish this. Just swap your brackets [0] for parentheses (0).
var m = 'My name is ';
function updateMsg(h) {
var s = ['Adam', 'Joseph'];
return function(index) { // return a function that concatenates when given index
return m + h + " " + s[index];
};
}
var messageMaker = updateMsg("Sarah");
console.log(messageMaker(0));
console.log(messageMaker(1));
I think you want to acces one element of the list of lastnames an pass the name. I have corrected your code and do something similar to what you want :
let m = 'My name is ',
s = ['Adam', 'Joseph'],
updateMsg = (h, i) => m + h + ' ' + s[i],
n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah", 0);
<p id="msg"></p>
Details are in the source in the comments.
SNIPPET
/*
Global variable: intro
*/
var intro = 'My name is ';
/*
Paramenter: who
*/
function updateMsg(who) {
/*
Array of 2 Strings: guess
Named it guess because that's
what I ended up doing with it
*/
var guess = [' Shadey', ' Joseph'];
/*
Concated String: `hello`
Since `intro` is global it's always accessible
`who` is the parameter with value of "Slim"
`guess[0]` = 'Shadey'
*/
var hello = intro + who + guess[0];
/*
Output of `updateMsg()` function is `hello`
which is "My name is Slim Shadey"
*/
return hello;
}
/*
Reference the `output` element as `noLogic`
*/
var noLogic = document.getElementById("msg");
/*
Set `noLogic's` text to whatever `updateMsg()`
returns
`updateMsg()` parameter is "Slim"
*/
noLogic.textContent = updateMsg("Slim");
<output id="msg"></output>
I guess the intent is to return an array with el prepended to each of the names in s. So you need to loop through the array to create the new array.
var m = 'My name is ';
function updateMsg(h) {
"use strict";
var el = m + h;
var s = ['Adam', 'Joseph'];
return s.map(function(name) {
return el + ' ' + name;
});
}
var n1 = document.getElementById("msg");
n1.textContent = updateMsg("Sarah")[0];
<div id="msg">
</div>

Get file from javascript

I'm not very familiar with js and now I need to do something very important for me, but I really don't know how to do it.
I'd like to include google translation api to my site, but I need to change some code in their js files. I have the element.js file on local host:
(function () {
var d = window,
e = document,
f = ".",
g = "UTF-8",
h = "complete",
k = "head",
l = "link",
m = "script",
n = "stylesheet",
p = "text/css",
q = "text/javascript";
Math.random();
function r(b) {
var a = e.getElementsByTagName(k)[0];
a || (a = e.body.parentNode.appendChild(e.createElement(k)));
a.appendChild(b)
}
function _loadJs(b) {
var a = e.createElement(m);
a.type = q;
a.charset = g;
a.src = b;
r(a)
}
function _loadCss(b) {
var a = e.createElement(l);
a.type = p;
a.rel = n;
a.charset = g;
a.href = b;
r(a)
}
function _isNS(b) {
b = b.split(f);
for (var a = d, c = 0; c < b.length; ++c) if (!(a = a[b[c]])) return !1;
return !0
}
function _setupNS(b) {
b = b.split(f);
for (var a = d, c = 0; c < b.length; ++c) a = a[b[c]] || (a[b[c]] = {});
return a
}
d.addEventListener && "undefined" == typeof e.readyState && d.addEventListener("DOMContentLoaded",
function () {
e.readyState = h
}, !1);
if (_isNS('google.translate.Element')) {
return
}
var c = _setupNS('google.translate._const');
c._cl = 'en';
c._cuc = 'googleSectionalElementInit';
c._cac = '';
c._cam = '';
var h = 'translate.googleapis.com';
var b = (window.location.protocol == 'https:' ? 'https://' : 'http://') + h;
c._pah = h;
c._pbi = b + '/translate_static/img/te_bk.gif';
c._pci = b + '/translate_static/img/te_ctrl3.gif';
c._phf = h + '/translate_static/js/element/hrs.swf';
c._pli = b + '/translate_static/img/loading.gif';
c._plla = h + '/translate_a/l';
c._pmi = b + '/translate_static/img/mini_google.png';
c._ps = b + '/translate_static/css/sectionalelement.css';
c._puh = 'translate.google.com';
_loadCss(c._ps);
_loadJs(b + '/translate_static/js/element/main_se.js');
})();
(If it's important, link to this file from web page is "element.js?cb=googleSectionalElementInit&ug=section&hl=en" )
And I need to get main_se.js (the last link in the file) on localhost too, but I don't know how to change link in element.js to this file to make it local. I need it, because I have to replace some html tags in this file to make api work properly for me.
Hope that somebody will advice me what to do.
If I understand correctly, elements.js produces a <script tag with src pointing to translate.googleapi.com and you want it to point to localhost.
The answer is quite easy in this case, simply remove the b+ as b is http://translate.googlapi.com you will get the following script tag
<script src="/transalte_static/js/element/main_se.js"></script>
All you have to do now, it make sure you return the right file (your localhost copy) from this path.
Let me know if you need anything else.

JavaScript Link Extractor

I am interested in extracting links from sites where the links are dynamically generated with JavaScript and are essentially invisible in HTML source. For instance here is an example site where the links are inserted via a js menu:
http://www.stcroixwebsolutions.com/
When I hover with the mouse over the links, I see the links, but they are not discernible in HTML source.
I would like to output the links like so:
http://www.stcroixwebsolutions.com/?110000
http://www.stcroixwebsolutions.com/?110010
etc.
What do you recommend I use to extract these links?
You could try something like this... This will at least get you started!
http://jsfiddle.net/Qv4St/
function showLinks() {
var links = document.getElementsByTagName( 'a' );
var last = links.length;
var list = {};
// for each anchor...
for (var i = 0; i < last; i++) {
list[links[i].href] = i;
console.log(list);
//' - text=' + links[i].innerHTML + '<br>';
}
var linksList = document.getElementById( 'linksList' );
linksList.innerHTML = list;
} ​
var getLinks = function () {
"use strict";
var a = document.getElementsByTagName("a"),
b = a.length,
c = 0,
d = [],
e = "",
f = location.href;
f = f.substring(0, f.lastIndexOf("/"));
for (c = 0; c < b; c += 1) {
e = a[c].getAttribute("href");
if (typeof e === "string" && e.length > 4) {
if (e.charAt(0) === "/" || e.charAt(0) === "?") {
e = f + e;
}
d.push(e);
}
}
return d.join("\n") + "\n" + d.length + " total links";
},
myLinks = getLinks(); //myLinks variable will contain the desired output.
//To output to the console just replace the line with 'return' with this code:
//console.log(d.join("\n") + "\n" + d.length + " total links");
Run this code to return a list of all hyperlinks on the given page in a list with each result on its own line.
EDIT: I now convert relative links to absolute URIs.
There is a standard document.links collection that is all the links in a document. Simply iterate over that.

How can I modify the tumblr bookmarklet to post to a specific tumblr blog?

I have a couple blogs linked to my Tumblr account, but the bookmarklet always selects my "primary" blog (the first one in the list).
How can I modify the bookmarklet so that it will auto-select a specific blog? I would like to have multiple bookmarklet links, e.g. "Share on blog1", "Share on blog2" so that I don't have to manually select which blog to create the post in.
Default Tumblr bookmarklet looks like this:
javascript: var d = document,
w = window,
e = w.getSelection,
k = d.getSelection,
x = d.selection,
s = (e ? e() : (k) ? k() : (x ? x.createRange().text : 0)),
f = 'http://www.tumblr.com/share',
l = d.location,
e = encodeURIComponent,
p = '?v=3&u=' + e(l.href) + '&t=' + e(d.title) + '&s=' + e(s),
u = f + p;
try {
if (!/^(.*\.)?tumblr[^.]*$/.test(l.host)) throw (0);
tstbklt();
} catch (z) {
a = function () {
if (!w.open(u, 't', 'toolbar=0,resizable=0,status=1,width=450,height=430')) l.href = u;
};
if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0);
else a();
}
void(0)
Give the bookmarklet a 'channel_id' post parameter which is 'example_blog_name' in example_blog_name.tumblr.com
javascript: var d = document,
w = window,
e = w.getSelection,
k = d.getSelection,
x = d.selection,
s = (e ? e() : (k) ? k() : (x ? x.createRange().text : 0)),
f = 'http://www.tumblr.com/share',
l = d.location,
e = encodeURIComponent,
c = 'example_blog_name',
p = '?v=3&u=' + e(l.href) + '&t=' + e(d.title) + '&s=' + e(s) + '&channel_id=' + e(c),
u = f + p;
Using a combination of a user script, and a little tweaking to the bookmarklet, here's your solution:
Install this as a UserScript:
var selectOption = function (elem, value) {
var options = elem.options;
for(var i = 0; i < options.length; i++){
if(options[i].innerHTML === value){
elem.selectedIndex = i;
}
}
};
window.onload = function (){
if(location.href.indexOf('tumblr.com/share') !== -1){
selectOption(document.getElementById('channel_id'), location.hash.slice(1));
}
};
Save this as your bookmarklet after editing the BLOG_NAME variable. Type it exactly as it is in the dropdown. Also, you'll probably have to run it through UglifyJS to make it a bookmarklet.
javascript: var BLOG_NAME = 'Test',
d = document,
w = window,
e = w.getSelection,
k = d.getSelection,
x = d.selection,
s = (e ? e() : (k) ? k() : (x ? x.createRange().text : 0)),
f = 'http://www.tumblr.com/share',
l = d.location,
e = encodeURIComponent,
p = '?v=3&u=' + e(l.href) + '&t=' + e(d.title) + '&s=' + e(s),
u = f + p;
try {
if (!/^(.*\.)?tumblr[^.]*$/.test(l.host)) throw (0);
tstbklt();
} catch (z) {
a = function () {
if (!w.open(u + '#' + BLOG_NAME, 't', 'toolbar=0,resizable=0,status=1,width=450,height=430')) l.href = u;
};
if (/Firefox/.test(navigator.userAgent)) setTimeout(a, 0);
else a();
}
void(0);

Categories