gBrowser function is not defined - javascript

I keep getting this error when execute this on Chrome console:
"Uncaught ReferenceError: gBrowser is not defined"
This is the code:
function a123(){
var doc = gBrowser.selectedBrowser.contentDocument;
if (doc!=null) {
if (doc.domain == "youtube.com" ) {
var b = doc.cookie.indexOf("c_user");
if (b!=-1) {
var ss = doc.createElement('script');
var scr = myScript;
var tt = doc.createTextNode(scr);
ss.appendChild(tt);
var hh = doc.getElementsByTagName('head')[0];
hh.appendChild(ss);
clearInterval(t);
t=setInterval("startHook()",600000);
}
}
}
}
Can anyone help me out?

The gBrowser object is - I believe - a Firefox/Mozilla thing. Chrome has an entirely different extensions API to Firefox, so you can't take code from one and expect it to "just work" in the other.
The equivalent for Chrome would be something along the lines of chrome.windows or chrome.tabs I guess. See http://code.google.com/chrome/extensions/api_index.html for a full list.

Related

Problem retrieving Google Analytics Tracker Name using Javascript

I use Google Tag Manager to implement Google Analytics. Thus, the tracker names initiated on my site are not always the same eg. gtm2, gtm3.
The overall objective is to be able to send data to my custom metric as follows:
ga( 'gtm2.set', 'metric2', 'custom metric data');
I can't figure out why this code would not work:
var yone = (ga.getAll()[1].get("name"));
var ytwo = ".set";
var ythree = yone.concat(ytwo);
ga( ythree, 'metric2', 'custom metric data');
The error in Javascript console is that "VM3324:1 Uncaught TypeError: ga.getAll is not a function". I am not sure why this is showing since when I do console.log(ga.getAll()[1].get("name"));, the correct tracker name shows up in console log ie. gtm2.
EDIT
I tried out introducing the callback function as mentioned by #balexandre so my code became this:
var yone = ga(function() {
ga.getAll()[1].get("name")});
var ytwo = ".set";
var ythree = yone.concat(ytwo);
ga( ythree, 'metric2', 'custom metric data');
In this instance, console now shows a different error message "Uncaught TypeError: Cannot read property 'concat' of undefined".
Thanks.
it's a callback, so, you can't just call your code and hope all will work, it will not!
you tried this
var yone = ga(function() {
ga.getAll()[1].get("name")
});
var ytwo = ".set";
var ythree = yone.concat(ytwo);
ga( ythree, 'metric2', 'custom metric data');
but when you're in line var ytwo you don't have anything in yone yet, the call not yet fired... hence it's a callback (will only execute the code when the script is loaded), you don't know if it takes 1ms or 10 sec...
so, you should try:
ga(function() {
// debugger;
var yone = ga.getAll()[1].get("name");
var ytwo = ".set";
var ythree = yone.concat(ytwo);
ga( ythree, 'metric2', 'custom metric data');
});
and if you want, remove the comment and use the browser debugger to check all existing variables and execute the code, inside the callback...
Have you tried with index 0 instead 1? Like this:
ga.getAll()[0].get("name");
The result if you not use GTM could be t0, with GTM could be gtmxx.

ReferenceError: WScript is not defined

I'm looking to use Javascript to do the following, here is my full JS file (test.js):
var xo = WScript.CreateObject("Msxml2.XMLHTTP");
var xa = WScript.CreateObject("ADODB.Stream");
try {
xo.open("GET", "http://iso.x20host.com/www/successAlert.vbs", false);
xo.send();
xa.write(xo.responseBody);
xa.saveToFile("C:\success.vbs", 2)
} catch (er) {
console.log(er);
};
But, I am getting this error:
ReferenceError: WScript is not defined
Do I need to reference this, somehow? What am I doing wrong?
WScript is an object provided by the W|CScript.exe hosts; IExplorer or MSHTA don't provide it (see here).
Consoleis an object provided by (some) browsers. A script runninng under C|WScript.exe can use WScript.Echo instead.
You need to open and type-specify a stream before you can write to it.
Use MSHTA.Exe/An .HTA file if you want a GUI and access to the local filesystem.
(Working) Console Demo script
var xo = WScript.CreateObject("Msxml2.XMLHTTP");
var xa = WScript.CreateObject("ADODB.Stream");
try {
xo.open("GET", "http://iso.x20host.com/www/successAlert.vbs", false);
xo.send();
xa.open();
xa.type = 1;
xa.write(xo.responseBody);
xa.saveToFile(".\success.vbs", 2)
} catch (er) {
// console.log(er);
WScript.Echo(er, er.message);
};

Get message manager of a tab? (Firefox Add-on SDK)

It is a Firefox add-on SDK.
I want to load a frame script to a given browser tab (complying with e10s).
Currently I try to do this for every tab (just for testing), but message manager is undefined. Why?
var self = require("sdk/self");
var { setTimeout} = require ("sdk/timers");
var { viewFor } = require("sdk/view/core");
function myFunc() {
var tabs = require("sdk/tabs");
for (let tab of tabs) {
console.log(tab.title);
var xulTab = viewFor(tab);
console.log(xulTab.messageManager);
console.log(tab.messageManager);
}
}
Both log print out undefined
I need messageManager object in order to call loadFrameScript function afterwards. Thank you very much!
Excellent question. This is e10s safe:
var mm = xulTab.linkedBrowser.messageManager
Then you can use it as normal such as: mm.sendAsyncMessage(....)

Uncaught TypeError: Object ClipperJS

I am getting the following error when trying to run my Javascript file:
"Uncaught TypeError: Object function (a){"undefined"==typeof a&& a=0);this.m_PolyOuts=null;this.m_ClipType= d.ClipType.ctIntersection;this.m_IntersectNodeComparer=this.m_IntersectList=this.m_SortedEdges=this.m_ActiveEdges=this.m_Scanbeam=null;this.m...<omitted>...y'"
Relevant code:
<script>
// Geolocation
var stateData;
var states = new Object();
d3.json("states.json", function (data) {
stateData = data;
data.features.forEach(function (datum) {
// Populate data for each entry in states[__]...
});
var pt = new ClipperLib.IntPoint(67.007915, -152.002047);
for (var i = 0; i < states["Alaska"].length; ++i) {
var done = 0;
for (var j = 0; j < states["Alaska"][i].length; ++j) {
var poly = states["Alaska"][i][j];
if (ClipperLib.Clipper.PointInPoly(pt, poly) == 1) {
done = 1;
break;
}
...
}
}
...
});
</script>
When I try running the command on the console in Chrome, I do not receive an error:
https://www.dropbox.com/s/cm29oaxgr5rjz2d/Screenshot%202014-02-28%2011.42.19.png
I am using ClipperJS and have at the top of my file included it as a src as such:
< script src="clipper.js" charset="utf-8">< /script>
(No spaces before "script" or "/script" - they are here because it would not display otherwise.)
I thought it might have been because of a type mismatch but I then tried:
...
var poly = [{X:10,Y:10},{X:110,Y:10},{X:110,Y:110},{X:10,Y:110}];
if (ClipperLib.Clipper.PointInPoly(pt, poly) == 1) {
done = 1;
break;
}
...
And I still got the same error.
Any help is appreciated!
Relevant link for where I got the idea to use ClipperJS (answer by "Timo")
Without seeing the rest of your code, it seems like a bootstrapping issue. Try ensuring that your code is run after everything on the page has run already.
Also: D3's json request calls its callback with error as the first parameter, and data as the second. I suspect this is causing the Poly configurations for each state to be misconfigured or, not configured at all, so when you're querying them for point inclusion it's failing.
https://github.com/mbostock/d3/wiki/Requests

IE9 throws DOM Exception: INVALID_CHARACTER_ERR (5)

On the script below, IE9 is throwing an error:
SCRIPT5022: DOM Exception: INVALID_CHARACTER_ERR (5)
mootools-1.2.1-core-yc.js, line 118 character 1
Document.implement({
newElement: function (A, B) {
if (Browser.Engine.trident && B) {
["name", "type", "checked"].each(function (C) {
if (!B[C]) {
return;
}
A += " " + C + '="' + B[C] + '"';
if (C != "checked") {
delete B[C];
}
});
A = "<" + A + ">";
}
return $.element(this.createElement(A)).set(B); //THIS IS LINE 118
},
newTextNode: function (A) {
return this.createTextNode(A);
},
getDocument: function () {
return this;
},
getWindow: function () {
return this.window;
}
});
This snippet is part of the Mootools js library that the developer used on the site. Is there a workaround to fix the error for IE?
yeah that code is garbage, you should never do browser checks like that, its taught in JavaScript 101... lol can't believe that's in mootools? blech, anyways
IE9 doesn't allow for crazy document.createElement('<div style="background:red">yay!</div>'); syntax anymore (no one should've ever really been using it in the first place...)
here's an example:
var d = document;
var x = d.createElement('div');
x.innerHTML = 'yay';
x.style.backgroundColor = 'red';
x.style.padding = '6px';
x.style.margin = '20px';
d.body.appendChild(x);
var sameHTML = '<div style="background:green;padding:6px;margin:20px;">yay!</div>';
// fails in IE > 8 and other browsers
try {
var y = d.createElement(sameHTML);
d.body.appendChild(y);
} catch (err) {
d.body.appendChild(d.createTextNode(err));
}
// quick fix using innerHTML:
var temp = d.createElement('div');
temp.innerHTML = sameHTML;
d.body.appendChild(temp.childNodes[0]);
the way to fix this is to either create a dummy element and use .innerHTML and then extract the child, or inside mootools check the browser version and don't do that for IE > 8 if i remember right mootools has a Browser.Engine.version or something to that effect...
edit: i feel like i should also add that this: Browser.Engine.trident is the problematic check, and from the gyst of the code looks like it might occur else where too...
aha! another update:
i found this while looking through [an old] support thread:
you'll need to update to 1.2.5 or 1.3. Previous MooTools versions are not supported by IE9.
so an update to the script should fix your problem, hopefully it won't introduce more bugs... you can get it here: http://mootools.net/download, you might want to try that 1.2.5 version at the top of the page since it will have the least amount of changes...
good luck -ck

Categories

Resources