Questions and answers from the newsgroups 2006 11 24

Return to mozilla-dev-extensions


FAQ

(↑ top)

How to make Sidebar work like a normal Page, with Ctrl-Click for new Tabs and Right-Click for Context Menu?
Put the file with the links in an HTML file, and included it like this.
<iframe id="gj53run-sidebar-iframe" flex="1"
src="chrome://gj53run/content/CookBook/MySidebar.htm"/>
example xul file
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://gj53run/skin/gj53run.css"
type="text/css"?>
<!DOCTYPE window SYSTEM "chrome://gj53run/locale/gj53run.dtd">
<window
xmlns="http://www.mozilla.org/keymaster/gat...re.is.only.xul"
                               xmlns:html="http://www.w3.org/1999/xhtml"
                               id="gj53run-ShowMenu-sidebar"
       onload="gj53runSidebarMenuOnload()"
       onunload="gj53runSidebarMenuOnUnload()">
<script type="application/x-javascript"
src="chrome://gj53run/content/utilsxul.js"/>
<script type="application/x-javascript"
src="chrome://gj53run/content/gj53runMenuXUL.js"/>
<iframe id="gj53run-sidebar-iframe" flex="1"
src="chrome://gj53run/content/CookBook/MySidebar.htm"/>
</window>
Then something like this in your overlay will work
<broadcaster  id="gj53run-xxh-sidebar"
                     autoCheck="false"
                     label="&gj53run.menuitem.SFT2.label;"
                     type="checkbox" group="sidebar"
                     sidebarurl="chrome://gj53run/content/xxxx.xul"
oncommand="toggleSidebar('gj53run-xxxxx-sidebar');"/>
If you follow the links from www.cookdotcom.com you can download my alpha extension.
There is a working example their, it the main menu
How to create a log when a user visits a site in Firefox?
Add an event listener to the window object to call your function when a browser window as loaded.
       window.addEvenListener("load", MyExtension.init, false);
In that function, get the current URL. If it's what you're looking for, create write to a log file.
For more info, check:
       http://developer.mozilla.org/en/docs/DOM:window (window.location)
       http://developer.mozilla.org/en/docs...ppets:File_I/O 
Is it possible to Detect the presence of specific extension from JavaScript?
You could use the DOM events to talk to the installed extension, see http://forums.mozillazine.org/viewto...005555#1005555 for description and http://circum.com/startalocalprogram.html for an example.
It is also possible to make an object accessible to webpage JS via a global property - see nsSidebar.js
You could create a dummy plugin (and install it together with itunes) which would then be visible via navigator.plugins <http://developer.mozilla.org/en/docs...igator.plugins>
Not to mention this url from some obscure hardware company. <http://developer.apple.com/internet/.../detectplugins...>
You could also append a string to the useragent. See http://www.mozilla.org/build/revised....html#implemen... for details.
How to create an extension to get selected text from browser?
See window.content here:

http://developer.mozilla.org/en/docs...window.content

See window.getSelection here:

http://developer.mozilla.org/en/docs...w.getSelection

 window.content.getSelection();
 note that window.getSelection() returns nothing at least in Firefox 2.0.0.3!
Is there a BrowserInitialized event?
Use the strategy of attaching an init script to the window.onload event:
<script type="application/x-javascript">
  <![CDATA[
     window.addEventListener('load', MyFunction.init, false);  
  ]]>
</script> 
For tabs, attach a listener to the tab container:
//attach listener to new tab in seamonkey brower
   container = gBrowser.mPanelContainer;
   container.addEventListener("DOMNodeInserted", MyFunction.processPage, true); 
How to control flashmovies with a firefox extension?
Make certain to use Mook's javascript function to test if the movie is 100% loaded before calling stop. Possibly you could use the setTimeout and setInterval functions from an extension to check at a certain interval and probe the Flash movies for their loading status. Once the loading is 100% then call stop. These routines could be started from a window.onload script in your extension.
// Checks if movie is completely loaded.
// Returns true if yes, false if no.
function movieIsLoaded (theMovie) {
 // First make sure the movie's defined.
 if (typeof(theMovie) != "undefined") {
   // If it is, check how much of it is loaded.
   return theMovie.PercentLoaded() == 100;
 } else {
   // If the movie isn't defined, it's not loaded.
   return false;
 } 
If you are accessing webcontent from chrome, you may need to use the JSWrappedObject property to access unsafe methods.
Is there a way around library conflicts between extensions?
In general you should set all your extension methods and global variables prefixed by a namespace to avoid conflicts. So if you copy mochikit and make changes then wrap all the functions in a unique namespace.
Example:
MyExtension.getMethod(). This will prevent collisions with other libraries with methods with the same name.
var ExtensionName = {
 onLoad: function() {
   // initialization code
   this.initialized = true;
 },
 onMenuItemCommand: function() {
   window.open("chrome://helloworld/content/hello.xul", "", "chrome");
 } 
You could also try creating a singleton object and then using mozIJSSubscriptLoader to load your library into the context of the object.
How to insert text into textfield?
<textbox id="myTB"/>
document.getElementById("myTB").value = "hello" 
Create an extension with an overlay that adds a popupmenu with the menuitems you want. On the window.onload event create an array of the textfields. On an html page the element would be an input tag with an attribute of type="text". For each textfield in your array add a context="myPopupID" to the input element. Now when the user right-clicks on a textfield your popupmenu from your overlay will appear. You could get the parent of the popupmenu which would be the textfield and then fill the textfield with the value of the menuitem selected.
You can easily do this by using Firefox's gContextMenu object as follows:
       gContextMenu.target.value += "Hello"; 
How to reference content.document javascript var from sidebar?
You could use the windowmediator to get the main window.
const windowMediatorIID = Components.interfaces.nsIWindowMediator;
const windowMediator =
Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(windowMediatorIID);
const topWin = windowMediator.getMostRecentWindow("navigator:browser");
winTitle = topWin.document.title 
http://developer.mozilla.org/en/docs/XPCNativeWrapper
How to modify entries on the LDAP server?
The nsILDAPSyncQuery actually interfaces to the code in: http://lxr.mozilla.org/seamonkey/sou...ry/xpcom/base/
which is a wrapper around the LDAP C-SDK (http://wiki.mozilla.org/LDAP_C_SDK).
Although the LDAP C-SDK has full read and write functionality, the xpcom wrapper hasn't yet got functions for being able to write to ldap directories (https://bugzilla.mozilla.org/show_bug.cgi?id=124897).
Is there some way to get a function to run when a user changes the page?
Try adding a listener for the "beforeunload" event.
Is it possible to get the event if a mail has been received in thunderbird?
Check out an extension called Growl Notify or something like that.
Add a listener to the MailSession and listen to the onItemAdded event. The OnItemAdded function must be

wrapped in the folderListener. It looks roughly like this:

OnItemAdded: function(parentItem, item, viewString) {
       if (parentItem instanceof Components.interfaces.nsIMsgFolder) {
               if (parentItem.flags & MSG_FOLDER_FLAG_INBOX) { // check inboxes only
                       if (item instanceof Components.interfaces.nsIMsgDBHdr) {
                               if (!item.isRead && (item.flags & 0x10000)) { // check new items only
                                       doSomething(item);
                               }
                       }
               }
       }
}
function register4MailEvents() {
       var mailSession = Components.classes["@mozilla.org/messenger/services/session;1"].getService(Components.interfaces.nsIMsgMailSession);
       var notifyFlags = Components.interfaces.nsIFolderListener.added;
       mailSession.AddFolderListener(spamatoFolderListener,notifyFlags); 
}
You can listen for mail:biff-state-changed observer notifications, the facility has only recently been added so if you use that technique your extension will only work in Thunderbird 2.
Alternatively, use a root folder listener, but listen for the integer BiffState property; the value is 0 for new mail.
The mail session collects all the notifications for all accounts. In fact, the individual account listeners are not readily available from JavaScript.
(Update 2007-05-05) Thunderbird2 gives us the nsiMsgFolderNotificationService interface, which is probably the best way to do this. It would look something like (add your own try/catch):
var threadBubbleListener = {
    itemAdded: function(item){   
        alert("Got mail.  Look at item's properties for more details.");
    }
}

function threadBubbleInit() {
    var notificationService = Components.classes["@mozilla.org/messenger/msgnotificationservice;1"].getService(Components.interfaces.nsIMsgFolderNotificationService);
    notificationService.addListener(threadBubbleListener); 
}

UAQ

(↑ top)

Document Tags and Contributors

Contributors to this page: Wj7Xhf, Darac, Mckwan, Clouserw, Dirtybear, Ptak82
Last updated by: Ptak82,