FireFox. How to create a bootstrap extension? - javascript

It isn't a coding question(I think :)). I have read manual about how to add bootstrap in to my FireFox extension, but what doing next?
I added bootstrap.js, modify install.rdf, what to do next?
What I need to do wiith my files in content folder?
My extension structure:
./chrome.manifest
./Icon.png
./Install.rdf
./chrome/content/Overlay.xul
./chrome/content/Overlay.js
./chrome/content/Options.xul
./chrome/content/Options.js
./chrome/content/Window1.xul
./chrome/content/Window1.js
./chrome/locale/<locales>
I written FireFox extension that's need browser restart, but restart isn't needed for extension work. I want remove that action, and I found something about bootstrap.
I think, now I need manually (un)register my windows, overlays, locales, where read about that?
Please, give me more info, url, links, how to modify my extension into bootstrapped.

Some chrome.manifest directives, e.g. chrome, skin, locale, are supported in bootstrapped extensions, so nothing special you need to do there.
Overlays are not supported, however. You need add some code that will use the DOM APIs to manually insert and/or change elements you normally would have in your overlay.
The "Further reading" section of the docs you already linked contains some stuff. Most bootstrapped add-ons I encountered use some variation of the watchWindows stuff.
It might be also helpful to look at real world examples, such as Restartless Restart (which happens to be rather small, and easy enough to understand).

Related

How to save changes to modifications of external site JS Code

I have recently been trying to mess around with JS code of different websites to explore the programming language further. I have been able to successfully modify the scripts of the websites but I cannot find a way to save the changes and they do not take effect. I am using the newer Microsoft Edge (Version 92.0.878.0 (Official build) dev (64-bit)).
It shows that "changes to this file were not saved to the system", how do I go about this?
(please note: this website is external and I have tried downloading the HTML and modifying it but then the site does not function)
This is the default behavior if you don't add the files to workspace. If you want to save the changes to file system, you need to use Filesystem-> Workspace.
You can refer to Edit files with Workspaces about the detailed steps of using Workspace. After Step 1: Set up, you can directly go to Step 4: Save a JavaScript change to disk.
Besides, there's another workaround to test the changed js code without saving to file system. You can refer to this answer about how to achieve this.
Windows file system is preventing chrome from saving file with special chars. Chrome is using filenames to match files on your local version with ones on the webpage.
Because of windows replacing special chars ("?") With escaped ones ("%3"), chrome is unable to match them and shows that error.
I don't know the perfect solution for that, but one of the workarounds would be to remove cache canceling string with question mark. Very nasty bug of local overrides.

Buttons not showing up in Firefox WebExtension notifications

I am working on a Firefox WebExtension in which I am trying to display a notification with a button. I have this working in Chrome. According to the docs, this is supported in Firefox. Buttons are listed as Optional in the NotificationOptions.
chrome.notifications.create(notificaitonId, {
type: "basic",
iconUrl: chrome.extension.getURL("images/unknown.svg"),
title: "Blah",
message: "A Message",
buttons: [{title: "Get More Details"}]
});
When I run this code, I see the notification. But, I don't see a button. Am I missing something? Is it actually not supported? The only example for notifications doesn't use buttons so that hasn't been helpful.
Unfortunately, buttons are not, as of 2016-03-18, implemented for WebExtension notifications.
The NotificationOptions documentation on MDN states [emphasis/formatting mine]:
The first four properties - type, iconUrl, title, message - are
mandatory in notifications.create(), and optional in
notifications.update(). Firefox currently supports only these
four properties.
and in the Browser compatibility section:
Firefox only supports: type, iconUrl, title, message.
The important parts of this ("Firefox currently supports only these four properties" and Firefox only supports: type, iconUrl, title, message.) were added to the documentation on 2016-03-07. Thus, if you looked at the documentation prior to that date there would have been no indication that buttons was not yet implemented.
In addition, the source code has the comment:
// FIXME: Lots of options still aren't supported, especially
// buttons.
You can also find the source code that is currently in use for this in your browser at: chrome://extensions/content/ext-notifications.js
Solution: Implement buttons yourself:
In WebExtensions:
It might be possible to implement buttons yourself from within WebExtensions. However, I am not familiar enough with WebExtensions to be able to say if it is possible, or not. If so, you will probably want to take a look at the source code that implements notifications and notification buttons for other types of Firefox add-ons.
For Firefox, generally:
If it was something I needed, I would probably choose to go ahead and implement buttons, and any other options I needed, for the entirety of Firefox and submit the code to Mozilla for review and inclusion in Firefox. I know this might sound like it is a big deal and a pain in the rear, but it really shouldn't be that difficult.
If you do desire to work on it, it should not take any special setup. The JavaScript code which implements notifications for WebExtensions is the file ext-notifications.js which is contained in the chrome\toolkit\content\extensions directory within the omni.ja file (just a .zip file with the extension changed to .ja) which is located in the Firefox install directory. Note there are actually 3 different omni.ja files within the install directory hierarchy. The one you want is in the root of install hierarchy, not the ones in either the browser or webapprt sub-directories.
Working on it would be as simple as extracting that file, modifying it, making an updated omni.ja file and putting the updated omni.ja file into the release directory. For a while, I did this routinely upon every release of Firefox because I wanted a longer bookmark MRU file list. I eventually broke down and just made an overlay extension, Change Bookmark Recent Folder List Length, to replace the file I was making changes to. If you want, you could do the same thing to implement the changes needed to support buttons. That would, of course, result in your current extension being dependent on the other one being installed until such time as the code was integrated into Firefox. But, it would result in both your having the functionality you desire now for your development, and the functionality getting into Firefox at least somewhat faster than waiting for someone else at Mozilla to implement it. An overlay extension like that just takes a install.rdf file, a two line chrome.manifest file and the updated ext-notifications.js file you are wanting to override/replace over the current one.

Taking screenshot using javascript in chrome packaged app

I need to grab the current html inside a chrome packaged app. I am aware of Taking screenshot using javascript for chrome extensions but when I tried this, chrome states that the "tab" permission is not allowed for packaged apps.
Is there any way I can reliably grab the current page?
You cannot do what you are trying to do with a packaged Chrome application. Instead, you should probably look at making an extension, as that would be the way to go if you want to interact with the current page.
If you're unsure, here's a resource for figuring exactly what you should choose: https://developer.chrome.com/webstore/choosing
Not from a chrome extension, as those are browser-centric solutions (not webpage-centric).
Niklas Von Hurtzen has created a very useful js extension called html2canvas that essentially does what you're looking for, just not as a chrome extension. You can see it here.
Once "rendered," the plugin will create a canvas element by traversing the DOM tree of whatever you hand to the extension. From there it's pretty simple... yourRenderedCanvasElement.toDataUrl() will return a data: URL representation of that canvas element, which by default is a PNG, giving you a "screenshot."
Edit: Ha, I see now that you reference this exact thing in the message thread in the other answer...

Understanding Firefox extension structure

I'm trying to write a Firefox extension that intercepts a certain HTTP request and return static content without the request making it to the actual server (similar to AdBlock).
I've looked up the tutorials and I've got a basic file layout. I've also worked out that I need to use the nsITraceableChannel API and add an observer to do what I want and I have example code for that.
Problem is, where do I actually put this code? And when is my extension actually loaded and executed? Is it running constantly and asynchronously in the background or is it loaded per page view?
The documentation doesn't seem very clear on this. This extension won't need a GUI so I don't need the layouting XUL files (or do I?). I tried writing some XPCOM (I don't think I did it right though) component, registered it in chrome.manifest but it doesn't seem to run.
Can anyone explain exactly how the Firefox extensions work and where should I put my actual JavaScript code to monitor requests? Or have I got the whole idea of what an extension is wrong? Is there a difference between add-ons, extensions and plugins?
Concerning the difference between add-ons, extensions and plugins you should look at this answer. But in general, you seem to have the correct idea.
The problem is, there are currently three very different types of extensions:
Classic extensions (not restartless): these will typically overlay the browser window and run code from this overlay. Since there is one overlay per window, there will be as many code instances as browser windows. However, classic extensions can also register an XPCOM component (via chrome.manifest as of Gecko 2.0). This component will be loaded on first use and stay around for the entire browsing session. You probably want your component to load when the browser starts, for this you should register it in the profile-after-change category and implement nsIObserver.
Restartless extensions, also called bootstrapped extensions: these cannot register overlays which makes working with the browser UI somewhat more complicated. Instead they have a bootstrap.js script that will load when the extension is activated, this context will stay around in background until the browser is shut down or the extension is disabled. You can have XPCOM components in restartless extensions as well but you will have to register them manually (via nsIComponentRegistrar.registerFactory() and nsICategoryManager.addCategoryEntry()). You will also have to take care of unregistering the component if the extension is shut down. This is unnecessary if you merely need to add an observer, nsIObserverService will take any object implementing nsIObserver, not only one that has been registered as an XPCOM component. The big downside is: most MDN examples are about classic extensions and don't explain how you would do things in a restartless extension.
Extensions based on the Add-on SDK: these are based on a framework that produces restartless extensions. The Add-on SDK has its own API which is very different from what you usually do in Firefox extension - but it is simple, and it mostly takes care of shutting down the extension so that you don't have to do it manually. Extensions here consist of a number of modules, with main.js loading automatically and being able to load additional modules as necessary. Once loaded, each module stays around for as long as the extension is active. They run sandboxed but you can still leave the sandbox and access XPCOM directly. However, you would probably use the internal observer-service module instead.

How can I debug a minified JS in firebug?

I have a web page which includes insane amount of minified JS files. The web page works perfectly fine on my local network but throws some JS error on staging. There is an issue in JS and I wan't to debug it. When I load the JS in Firebug's script tag it appears in one long horizontal line. Is there a way out in Firebug that expands or beautifies the script for debugging? I know I can use jsbeautifier but they wont help me. I can not upload an expanded file to CDN, defeats the purpose of using CDN.
Points to be noted,
a) I can not control the box which serves JS, its on CDN, I mentioned it.
b) I can use beautifiers etc but would that help me in debugging the script in run time? IMHO, no
c) Being bound by NDA and other legal things I can not share the script but its a generic problem, you can encounter it with a minified jQuery
Beautify your script
Add the CDN host in /etc/hosts or your local DNS to resolve it to your own web server
Host the beautified version and everything that you need on said web server
Both Firefox and Chrome (versions as of this edit) support beautifying script with the {} button available in the inspector.
Just load the minified file and press the {} button at the bottom and it instantly beautifies, making breakpoints and other debugging possible. (True for Chrome too)
This is a common problem and the Chrome dev team have recently come up with an elegant solution, which they've called Source Maps - see http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/ for more info, I think you'll find it's exactly what you (and the rest of us) have been crying out for! :)
This is more a workaround, but it can help. The idea is that we will replace files coming from the server by files on your machine.
This will work with any browser.
It takes a bit of setup the first time (15 minutes maybe), but then it can be very convenient.
It can also helps testing your bug-fixes in a live/prod environment.
Get Fiddler (it's a web debugging proxy), install it, run it.
http://www.fiddler2.com/fiddler2/
(Restart browser after install to get the Fiddler extension)
If you debug an HTTPS website, check this first:
http://www.fiddler2.com/Fiddler/help/httpsdecryption.asp
From now on, you should see in Fiddler ("Web Sessions" pane on the left) all downloads made by your browser, including JS files.
If not, check this : Fiddler not displaying sessions
Find the file you want to debug in the list (Ctrl+F works)
Click on the file. Then either:
get the file content from the inspectors pane (textView tab), beautify it, save to a file on your local computer
or have access to a file which contains the source code (ex: from your source control)
Go to AutoResponder tab (top left pane).
Select "Enable automatic responses" checkbox.
Select "Unmatched requests passthrough" checkbox.
Drag your file from left pane to right pane (prefills rule editor at the bottom)
Set the other field with the path of your local file
Click the Save button
Reload the page and enjoy your debugging session.
Fiddler can do many more things, but this use-case answers the initial question.
Consider a Change!
Firefox w/ Firebug was my favorite JavaScript debugging method for almost a year, but I've recently moved to Google-Chrome's Developer-Tools which is far more superior.
Chrome supports an On-The-Fly (built-in feature) beautification of JavaScript resources
Once beautified, you are free to debug the JavaScript resource file, as it was "natively" downloaded beautified from the web-server. Breaking-points are set by clicking the line number.
One of the most extremely powerful feature,
Is once You've Stopped In A Breaking-point, You Are Free To Execute Commands (using console) In The Same Scope You ARE In The Breaking-point. In Firefox you can't do that.
Its so easy to debug (even anonymous functions), You'll never be back to Firefox.
Try It!
Pretty-print your JavaScript. Google this and you'll find multiple on-line JS beautifiers.
I happen to use http://jsbeautifier.org/ myself and it works fine, but search for others and use one that suits your needs.
Caveat: You still won't be able to get meaningful local variable names (which are usually renamed by a minifier). If the code was compiled by the Closure Compiler, then you absoutely won't get any useful information back at all, even when beutified, because then all variables and functions and properties are mangled (not only local ones).
Now, if your problem is with debugging code that comes from outside (e.g. a CDN), obviously that code would be minified, and you can't save your beautified version back there. In this case, you can replace the tags that load code from a CDN with a url pointing to your local version, then you can beautify the code (downloaded from the CDN) into your own server and you can then debug with FireBug.
Now, if you don't even control the HTML that contains those tags (e.g. they reside on a outside server), then unfortunately there is no way for you to do what you want without physically downloading the entire site to your own server. Even if you downloaded the entire site (with all the files), it may not work since the site may be driven by a back-end processing language or accesses a back-end database. In such case you'll also need to simulate all those data. It can be done, however -- you just have to go through a lot of pain. My recommendation is to save a version of the web page and run it on your own server, serving beautified code from your own server to debug.
Placing breakpoints on JavaScript makes debugging much easier, but if your code has already made it to production then it's probably been minified. How can you debug minified code? Helpfully, some of the browsers have an option to un-minify your JavaScript.
In Chrome and Safari, simply select the 'Scripts' tab, find the relevant file and then press the "{ }" (pretty print) icon located in the bottom panel.
In Internet Explorer, click the tool icon by the script selection drop down to find the option to format the JavaScript.
Opera will automatically prettify minified JavaScript. Source

Categories

Resources