How to debug Greasemonkey script with the Firebug extension? - debugging

I didn't find a way to debug Greasemonkey scripts with the Firebug extension.
Does anyone know how to do this ?
Thanks.

Updatier: The Mene+Shuman fix now is busted with Firefox 30 and Firebug 2. Firefox 31 may provide workarounds (will investigate). In the meantime, use the "General workaround strategies" listed below.
Update: This answer is now obsolete.
If you open about:config and
set extensions.firebug.filterSystemURLs to false
then you can use Firebug to debug the Greasemonkey script just like any other.
This works irregardless of the #grant mode.
See Mene's answer -- with an assist from Shuman.
Old answer:
Because Greasemonkey operates in a sandbox, Firebug cannot see it. There is no easy way around this.
General workaround strategies:
Test all parts of a GM script that don't use GM_ functions, in Firebug's JavaScript console first. Minimize use of GM_ functions and don't use GM_log() at all.
All of Firebug's console functions work great from within a GM script.

Note: this answer refers to old versions of Firefox. Firebug is no longer available, but lives on in the Developer Edition of Firefox.
Current Firefox and Firebug can now debug current Greasemonkey scripts just like any other javascript. Just find your *.user.js script in the dropdown menu. The console also works.
This works at least on Firefox 28.0 and Firebug 1.12.7; I haven't tried earlier versions.
Note: In order to get it to work, you probably have to set extensions.firebug.filterSystemURLs to false. See "Profiling Greasemonkey scripts" in the Firebug, bug tracker. (Thanks to Shuman)

var e = document.createElement("script");
e.src = 'http://www.xxxxxxxx.com/yyyyyyyy.js';
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);
you can add this to your xxx.user.js, and install it in greasemonkey.
Then, you can debug your js as you wish.

None of the other solutions here worked for me, but Jan Odvarko's answer on how to debug Firefox extensions worked perfectly for GreaseMonkey scripts as well:
On Firefox 19 or later, it's possible to use the built-in JS debugger
on the browser itself. Go to about:config and set the following two
prefs:
devtools.chrome.enabled: true
devtools.debugger.remote-enabled: true
After you restart the browser, you can access the Browser Debugger
through Tools > Web Developer > Browser Toolbox.
(note that you must accept the incoming connection)
See more at:
https://developer.mozilla.org/en-US/docs/Mozilla/Debugging/Debugging_JavaScript#JavaScript_Debugger
Then just search for the name of your userscript and start debugging.

It can be done using native Firefox debugger as it was mentioned before. Below is the instruction for modern versions of Firefox.
Set the following preferences in about:config:
devtools.chrome.enabled: true
devtools.debugger.remote-enabled: true
devtools.debugger.prompt-connection: false
Open the global script debugger window via
Tools → Web Developer → Browser Toolbox → Debugger (or Ctrl+Shift+Alt+I) .
Search for the name of your userscript and start debugging.

-- This answer is obsolete, please use #Brock Adams solution above --
Load your main script externally, instead of running it via GM. So you're just using GM to inject the script.
This is a bit of a hybrid between #bigml and #Yuval's solution and it uses jquery. It also works in frames.
// ==UserScript==
// #name My GM script
// #include The website I want this to run on
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==
$(document).ready(function() {
// fetch jquery dependent scripts using $.getScript()
});

Note: ChromeBug no longer exists. The Developer Edition of Firefox is an alternative.
Chromebug can see sandboxed scripts, http://getfirebug.com/wiki/index.php/Chromebug_User_Guide, but I've not tried it on Greasemonkey.

Similar to #bigml's suggestion, you can run it unprivileged if you setup a local webserver (apache) to serve the userscript file, then in your userscript add something along the lines:
if (typeof GM_addStyle == "undefined") {
loadScript("http://localhost/path/to/script.user.js");
}
else {
runScript();
}
function loadScript(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.getElementsByTagName('head')[0].appendChild(res);
}
function runScript() {
// ... whatever your userscript does ...
}
Of course you wouldn't be running in a privileged context. But this way you can easily continuously debug the script as any other script.

Note: this answer refers to old versions of Firefox. Firebug is no longer available, but lives on in the Developer Edition of Firefox.
I've tried ChromeBug, it doesn't seem to work.
With FireBug I have had the starting point of success by adding "debugger" to my GM code. This causes a breakpoint and I can inspect variables on the stack, but the right file is not shown so I can't step or anything.
I have had the best success with FirebugMonkey (https://
addons.mozilla.org/en-US/firefox/addon/13623/), which I just got working to do basic
debugging of GreaseMonkey scripts thanks to some explanation in a
recent comment on the extension page by f0rsvinn. Here are the instructions I just posted at http://groups.google.com/group/greasemonkey-users/browse_thread/thread/994cfa58c79d222:
It never occurred to me that the way it works is by creating its own
sandbox around the script rather than using Greasemonkey's, you
actually have to turn GM off. There are some GM aspect things that
will not work though because the script really isn't in GreaseMonkey.
As an example, GM_getValue returns undefined.
Still, it works for basic debugging - and is way better than nothing.
Usage steps are as follows:
Install FireBug 1.5.4 (later versions do not seem to work)
Install FireBugMonkey
Use the Script Manager in FireBugMonkey to select the files you want to debug
Disable GreaseMonkey (scripts will run inside FireBugMonkey, not
GreaseMonkey)
Enable FireBugMonkey
Enable scripts in FireBug
The scripts you added in the ScriptManager should be visible in the
FireBug scripts list.

As the others have said, you can setup a simple HTTP server and serve it to your page using Greasemonkey like so:
function loadScript(url) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.getElementsByTagName('head')[0].appendChild(script);
}
WEBrick and Python -m SimpleHTTPServer are good for this. We can also expose the GM_... functions to the script by adding a custom event handler to the document within GreaseMonkey:
function gMHandler(e){
GM_log(e.detail.message);
e.detail.response = "Hi!"
}
document.addEventListener("gM", gMHandler, false);
and then in the served script, raising this event on an arbitrary DOM element will run the handler and modify the element's response parameter:
$(document).ready(function() {
var event = new CustomEvent(
"gM",
{
detail: { message: "Hello World!" }
bubbles: true,
cancelable: true,
}
);
document.getElementById("AnyElement").dispatchEvent(event);
alert("Response was: " + event.detail.response);
});

Related

command line parameters for firefox

I am running program which open "chromium-browser" with following command-line parameters in Linux.
--incognito = Causes the browser to launch directly in incognito mode. ↪
--no-first-run = Skip First Run tasks, whether or not it's actually the First Run. Overridden by kForceFirstRun.
This does not drop the First Run sentinel and thus doesn't prevent first run from occuring the
next time chrome is launched without this flag.
--disable-save-password-bubble
--password-store = Specifies which password store to use (detect, default, gnome, kwallet).
--password-store=basic
--no-default-browser-check = Disables the default browser check. Useful for UI/browser tests where we want to avoid having
the default browser info-bar displayed.
--window-size = Specify the initial window size: --window-size=w,h
--window-size=1024,1024
--app = Specifies that the associated value should be launched in "application" mode.
Now I want to do the same thing with "firefox" browser. I need help to find firefox command-line parameter for firefox which is similar to chromium-browser.
I searched firefox website but not able to find alternatives for same.
Try to look them up here Mozilla - Command Line Options remember: Mozilla is the umbrella organisation of firefox, thunderbird, ... so this looks like the normative documentation place - the reality check then will be the installed binary, but then you can file issues in the tracker (if an option similar to these needed by you is documented but does not work).
Update 2021-08-30: Note that the options page is in archive status and there may be non-listed options available or listed options missing with newer versions. Until the Mozilla / Firefox project comes up with a maintained manual page on these options a good way to ensure what options are supported is to call firefox on the command line with the --help parameter.
You can try to check this one
this works as of 21/10/21
You would also want to look at the '-kiosk' argument
(similar to the chromium '--app' arg) which isn't really documented in the pages referenced in other comments here.
https://support.mozilla.org/en-US/kb/firefox-enterprise-kiosk-mode

How to debug firefox extension after cfx run?

I want to debug my firefox extension. I set
javascript.options.showInConsole = true
devtools.chrome.enabled: true
devtools.debugger.remote-enabled: true
run in sdk console cfx run, after that i go to Web Developer -> Browser Toolbox get incoming connection and i see my extension main.js. But after that, the code in main.js already been executed. How to debug it after cfx run?
also, two other things might be messing with your approach:
1) when you use cfx run, that by default creates a new profile on every run, so any settings that you have changed will not persist. to avoid this, you need to specify a profile directory with --profiledir=DIR (warning: don't use your main profile).
2) if the addon main.js code has already run by the time you open the debugger, you should start firefox manually, setup the debugger, and then drag the addon xpi into a tab.
Bug 899054 - [Meta] Implement an Add-on Debugger
this is really close to landing (the UI bits in bug 911098 are in m-c), so if you grab a Nightly tomorrow, or the day after, it should be in there, and might just work (for some undefined value of "work").

How would one display a prompt after installation of an extension?

When my add-on installs it needs to prompt the user to get a username or something like that. After that it stores it and shouldn't ask again. Where would I place this prompt? install.rdf? browser.xul?
There is no explicit mechanism to run code when the extension installs - you should simply do it when your extension runs for the first time. The easiest approach would be checking whether the user name is already set up. If it is not - show the prompt.
It is not recommended to show a modal dialog, those are extremely annoying to users, especially when they suddenly appear during Firefox start-up. You should instead open your page in a tab. A slight complication: Firefox might be restoring a previous session when it starts up. If you open your page too early the session restore mechanism might replace it. So you should wait for the sessionstore-windows-restored notification, something like this should work:
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
var observer = {
observe: function(subject, topic, data)
{
Services.obs.removeObserver("sessionstore-windows-restored", this);
var browser = window.getBrowser();
browser.loadOneTab("chrome://...", {inBackground: false});
},
QueryInterface: XPCOMUtils.generateQI([
Components.interfaces.nsIObserver,
Components.interfaces.nsISupportsWeakReference
])
};
Services.obs.addObserver("sessionstore-windows-restored", observer, true);
A final complication is that your code is probably running from a browser window overlay - meaning that there will be multiple instances of your code if the session restored contains more than one window. You probably want the code above to run only once however rather than opening your first-run page in every browser window. So you will have to coordinate somehow, maybe via preferences. A slightly more complicated but better solution would be having a JavaScript code module in your extension - code modules are only loaded once so you wouldn't have a coordination issue there.
Try using an addonlistener https://developer.mozilla.org/en/Addons/Add-on_Manager/AddonListener#onInstalling%28%29
Or by using the preferences: https://stackoverflow.com/a/958944/1360985

How to restart firefox from code?

How to correctly restart firefox (without any "restore session" things and with the same windows as before) from code?
I know pid of "firefox-bin" in a bash script process and I have my custom plugin loaded into it.
Services.prefs.setBoolPref("browser.sessionstore.resume_session_once", true);
const nsIAppStartup = Components.interfaces.nsIAppStartup;
Components.classes["#mozilla.org/toolkit/app-startup;1"]
.getService(nsIAppStartup)
.quit(nsIAppStartup.eRestart | nsIAppStartup.eAttemptQuit);
Note that this applies to Firefox 4 so the code might be slightly different for earlier versions.

Internet Explorer 8 64bit and Selenium Not working

I am trying to get selenium tests to run. Yet every time I try to run a tests that should run IE I get a error on line 863 of htmlutils.js It says that I should disable my popup blocker. The thing is I went to IE tools-> turn of popup block.
So it is disabled and I get this error.
Is there something else I need to disable. I actually don't even know what version of Internet explorer it is running since I am using Windows 7 Pro 64bit version. So when I do use IE I use 64bit version but I am under the understanding if the site or something like that does not support 64bit it goes to 32bit.
So not sure what I need to do it to make it work.
This is the lines where it does
function openSeparateApplicationWindow(url, suppressMozillaWarning) {
// resize the Selenium window itself
window.resizeTo(1200, 500);
window.moveTo(window.screenX, 0);
var appWindow = window.open(url + '?start=true', 'selenium_main_app_window');
if (appWindow == null) {
var errorMessage = "Couldn't open app window; is the pop-up blocker enabled?"
LOG.error(errorMessage);
throw new Error("Couldn't open app window; is the pop-up blocker enabled?");
}
Where is this log.error message stored? Maybe I can post that too.
I had a similar problem on Vista and IE8
I would get the same error message
Couldn't open app window; is the pop-up blocker enabled?"
Running my remote control as Admin wasn't an option for me, and also a poor idea from a security perspective.
So in the end I manage to solved this by changeing browser from "*ietha" to "*iexploreproxy"
grid_configuration.yml
hub:
port: 4444
...
- name: "Internet Explorer 8 on Vista"
browser: "*iexploreproxy"
...
Alternatively, you can change browser string from the code:
ISelenium selenium = new DefaultSelenium("localhost", 4444, "*iexploreproxy", "http://www.google.com/");
Works like a charm.
The only question remaing is if this somehow affects the outcome of the test cases. So far no, but I'll update this answer in case that would happen.
I ran into this on Windows 7 64bit.
My solution was:
Disable popup block. - Select "Tools/Popup Blocker/Turn off pop-up blocker"
Disable IE protected mode. - Untick "Tools/Internet Options/Security/Enable protected mode"
It'd be better just to disable protected modes for known trusted hosts/addresses. I'll leave that as an exercise for the reader.
I was experiencing the same problem. I ran the Selenium RC server as an administrator and everything worked fine.
I, too, am experiencing this very problem on a Windows 7 64bit box, trying to run Selenium on it to test and ASP .Net MVC application, written in C#.
I am still trying to work out the answer for myself, but I thought I'd post here to tell you of a little progress I have made in getting something to work, albeit in Firefox instead of IE.
Here's the line I changed:
selenium = new DefaultSelenium("localhost", 4444, "*chrome C:/Program Files (x86)/Mozilla Firefox/firefox.exe", "http://www.bbc.co.uk/");
I would ideally like for this to work in Internet Explorer 8, but if for the moment, I can begin getting tests working and later change over to use IE again, then great.
Hope this helps for your problem with it all.
I had the same problem on Windows 7 64bit IE8. The first step was to disable the IE popup blocker. Then, I got a message in the status bar saying that "Pop-ups were blocked on this page. Press the 'Ctrl' key to allow the pop-ups".
It turns out that the Google Toolbar was providing this feature. Disabling it solved the problem. View > Toolbars > Google to toggle.
John.
If you happen to be doing this from JavaScriptMVC, there is a reference you need to change in \jmvc\plugins\test\drivers\selenium.js:
1) Change iexplore to iexploreproxy and you should get better results:
msie : (/iexploreproxy/i).test(browserStartCommand),
2) At this point, you'll find that you still get the popup error, but a separate instance of IE has started. Leave that IE window open and restart the tests, but not Selenium.
3) Next, the windows should show up in the right place, but IE gives the annoying block active content warning. Allow the content to run and restart the tests, but not Selenium itself.
This is super clunky, but it at least gets you past that part. If I find more methodical ways to do these things I'll update as needed.
I have had the same problem and have found another solution which works for me. Just use the *iexploreproxy setting in the browserString.
I used:
selenium = new DefaultSelenium("localhost", 4444, "*iexploreproxy C:/Program Files/Internet Explorer/iexplorer.exe", "http://www.bbc.co.uk/");
I hope that works for others too :)
You can start the test when you disable the Security mode of Internet. Don´t know the correct name for it, but in dutch it is beveiligde modus.
I tried modifiing the security settings to dublicate this security mode, but couldn´t find the correct setting for it. It must therefor block more then you can set manually.

Resources