Backbone - accessing in jsfiddle console - javascript

I'm working with the event system and I wanted to run some quick commands in the console.
I noticed the global variable Backbone is available in the JavaScript window of www.jsfiddle.net but not from the console.
Here is the fiddle.
console.log('hello');
console.log(Backbone);
This is different behavior from what I'm use to.
When I type Backbone into the console ( I am in FF 14 ) I get:
ReferenceError: Backbone is not defined

That's the joy of iframes (or frames in general). Chances are good that the console panel you had was attached to the parent site and not to the iframe containing your code (and thus the Backbone object)

Related

How to use copy() in console if opened website has a function of the same name

With copy("text") in the developer tools console of Chrome and Firefox you can copy a "text" without an user interaction. Is this also possible if the website I've opened also has a function named copy? If yes, how?
If you refer to window overriding:
Unfortunately, once website overrides methods from window object, they are basically gone for good.
There are some workarounds, for instance you could use proxy interceptor like Charles, inject something like <script> window.copy_saved = window.copy; </script> on the very top of the <head> tag and then use copy_saved method with preserved original functionality.
If you refer to accessing functions defined in website's script tags:
It really depends on implementation, if the method is wrapped in structure accessible in window object, then yes, you can find a way to invoke the method. Methods however, can be stored in closure-like objects and thus being inaccessible by outside environment. It usually takes thorough reverse engineering to find a way into minified closures.

Can I mimic global window object in nodejs?

Edit I solved my actual problem in a way where this was no longer an issue, so I no longer need a solution to this. The question can remain though.
In a nodejs project I am analyzing dependencies by requiring them. This causes problems when some modules were built for the browser and expect window or document to be available. so for example, when I do require('some-module') I can get this kind of error:
hasEventListeners = !!window.addEventListener,
^
ReferenceError: window is not defined
Is there any way I can mimic the window to get this type of code to work? The properties on window does not need to work as the actual version, I just need require('some-module') to return a valid value.

Use content script to define global variables

I am creating a Firefox extension, and one feature of it that I would like is the ability for the user to inject a script or stylesheet into a specific website, rather like Greasemonkey (except that this will only be for one site). I am adding some functions for the scripts to make use of, which I intended to add from the Content Script into the main (unsafe) window. On the MDN blog, it says that they have made changes to how it should be implemented, so I have based my code on the new implementation as advised in the post, so this is what I have:
var $jq = jQuery.noConflict();//Yes, I am also injecting jQuery at the same time
console.log("created jquery object"); //This works
exportFunction($jq, unsafeWindow, {defineAs: "$jq"});
console.log("This will never be called");
But execution of the script just stops, and in the console it prints Message: TypeError: window is null.
I am testing in Firefox 28 predominantly (I can't seem to get Firefox for Ubuntu to update beyond that right now, and a whole load of issues are forcing me to use Ubuntu in a VM for this), but in Nightly 31a1 (Win7) nothing is ever injected, including a hardcoded style (that works on FF28) so I will have to figure that out at some point. (The PageMod code is here:
var lttWorker = sdk.pageMod.PageMod({
include:["*"],
/*contentScriptFile: [sdk.data.url("jquery.large.js"), sdk.data.url("scripts/bootstrapper.js")],
contentScriptWhen: "ready",*/ //This is commented to test whether it was an issue with the script. It's not.
contentStyle: "#header_bar{background-color:green;}", //This is injected in FF28 but not 31
attachTo: ["existing", "top"],
onAttach: function(){desktopNotifications({title:"attached content worker", text:"The content worker has been successfully attached"})} //This is called in FF28 but not 31
});
lttWorker.on("error", function(){callError("pageWorker failed");}); //This never gets called. Ever.
if anybody is interested)
EDIT: I have now tried it on Firefox 30b and there are still a load of issues, although they seem to be slightly different to both FF28 and 31...
First of all: These new functions are supported in Firefox 30 and later. See #canuckistani answer.
The exportFunction API is way too limited to actually inject something like jQuery with all the complex objects being or containing DOM nodes. That simply won't fly with the structured-clone algorithm that is applied to arguments.
The API is meant as a way for add-ons to communicate with pages bi-directionally, and not to inject complex libraries.
Your best bet is actually creating a script tag using the DOM APIs and putting jQuery there.

Javascript Alert message runtime error Object Expected

Ok .. this is a strange one as I have NOT seen this before. I have an application that is strictly a Service ... no browser involved ... and all I want to do is use alert(); for debugging. The only problem is that it causes an Object Expected error even if it is a simple alert("Show me!");
Remember ... this code is not attached to any form or browser. So what am I missing? I thought I could use the alert call at any time in Javascript ... Please folks, help a poor programmer out!
Thank you in advance,
Eric
Like praneeth already suggested in his reply, it is a Windows WScript thing, or rather just context in which the script is being run in.
This also works and isn't quite as verbose as what praneeth offered:
WScript.Echo("Hello");
if you are executing this script on a windows machine you can do like this in javascript/Jscript
Var Shell = new ActiveXObject("WScript.Shell");
Shell.Popup("Your Debug message");
The alert() method is one of the JavaScript browser Window object's methods which displays an alert box with a message and an OK button.
The window object represents an open window in a browser. If a document contain frames, the browser creates one window object for the HTML document and one additional window object for each frame.
I believe that in the specified case, the error means that the Window expected object has not been found.
Have you tried window.alert("show me");?
Since alert() is a Window object method.
If you're not running in a browser, you might have better luck using the console.log method - again, it's very hard to tell you specifically what to do without any detail of what environment you're executing the script in.

JavaScript objects visible in FireBug, inaccessible in code

In my code I have a line that dumps the current window (which happens to be a youtube video page):
Firebug.Console.log(myWindow);
It can be seen that window object contains "yt" property, which is another object that can be easily inspected in debugger:
http://i.imgur.com/lHHns.png
Unfortunately, calling
Firebug.Console.log(myWindow.yt);
logs "undefined" - why is that, and how can I access this "yt" property?
Edit: one addidtion that might be important: the code I'm writing is part of a firefox extension, so it's not really running inside a pgae, but in chrome - I'm starting to think that it may be the cause. Can chrome scripts be somehow limited in what they can see/acces as opposed to code in script tags?
For security reasons, Firefox extensions don't access web page objects directly but via a wrapper. This wrapper allows you to use all properties defined by the DOM objects but anything added by page JavaScript will be invisible. You can access the original object:
Firebug.Console.log(XPCNativeWrapper.wrappedJSObject.yt);
However, if you want to interact with the web page from an extension you should consider alternatives where the web page cannot play tricks on you (e.g. running unprivileged code in the content window: myWindow.location.href = "javascript:...").
Firefox and Chrome extensions can't access JavaScript within the page for security reasons.
I have seen confusion like this using asynchronous APIs.
console.log(obj); shows the contents of an object all filled in, but when accessing the object properties in code, they aren't really populated yet due to the call being asynchronous.
Why Chrome and Firefox shows them all filled in is probably just a timing issue as they probably process the console.log() asynchronously as well.

Categories