Tuesday, May 08, 2007

Using the findbar as a toolkit widget


<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>

<window id="FindbarTest for bug 304188 -
find-menu appears in editor element which has had makeEditable() called but designMode not set."
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
width="600"
height="600"
title="findbar test">

<browser id="content" flex="1" src="data:text/html;charset=utf-8,some%20random%20text" type="content-primary"/>
<findbar id="FindToolbar" browserid="content"/>
</window>

Open it under chrome by opening from a console and typing this: firefox -chrome chrome://tests/content/findbartest.xul.

test.xul file

And I use this as tests.xul file now, for easy file browsing:

<?xml version="1.0"?>
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
width="600" height="600">
<script type="application/javascript"><![CDATA[
function doe2(e) {
var str = 'chrome://tests/content/'+e.target.href.replace('file:///C:/testfiles/','');
e.target.href = str;
window.location = str;
e.preventDefault();
}

function doe() {
document.getElementById('content').contentWindow.addEventListener('click', doe2, true);
}
]]></script>
<browser id="content" flex="1" src="file:///C:/testfiles/" type="content-primary" onload="doe()"/>
</window>

Mapping a directory to a chrome url

I needed to write a unit test for bug 304188, and I didn't have any experience with that.
Unit tests expect to have chrome privileges. Normally I use enablePrivilege to get enhanced privileges, but for various reasons my test wasn't working correctly. So that's why I wanted to be able to use a chrome:// url to have enhanced privileges 'automatically'.
Normally, I rip an extension apart and put the file in question in the content directory of the extension, but this time I wanted an easier way.

According to this xulplanet, I could add a chrome.manifest file to my profile's chrome/ directory.
My profile directory is: C:\Documents and Settings\mw\Application Data\Mozilla\Firefox\Profiles\uulmz982.Default User
So I thought that adding the chrome.manifest file to C:\Documents and Settings\mw\Application Data\Mozilla\Firefox\Profiles\uulmz982.Default User\chrome would work. Apparently, that doesn't work and isn't supposed to work, so I heard.

My chrome.manifest file consists of this:
content tests file:///C:/testfiles/
So that means I can put content in C:/testfiles/ and can access them with chrome privileges from a certain chrome:// url.

Thanks to Mossop on #extdev, I heard what the 'correct' way is of doing this.
I did the following (finally, after a lot of tries)


  • - Create a folder named test@test at C:\Documents and Settings\mw\Application Data\Mozilla\Firefox\Profiles\uulmz982.Default User\extensions


  • - Create 2 files:

    • -- chrome.manifest, it consists of this:
      content tests file:///C:/testfiles/


    • -- install.rdf, it consists of this: (Mossop gave me this link: http://pastebin.mozilla.org/1839


      <?xml version="1.0"?>

      <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
      xmlns:em="http://www.mozilla.org/2004/em-rdf#">

      <Description about="urn:mozilla:install-manifest">
      <em:id>test@test</em:id>
      <em:version>1</em:version>

      <!-- Firefox compatibility -->
      <em:targetApplication>
      <Description>
      <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
      <em:minVersion>1.0</em:minVersion>
      <em:maxVersion>4.0a1</em:maxVersion>
      </Description>
      </em:targetApplication>

      <!-- Front End MetaData -->
      <em:name>Test Extension</em:name>

      </Description>
      </RDF>



    • Put those 2 files in the above mentioned test@test folder

    • Now you should be able to access for instance a 'test.xul' file from 'chrome://tests/content/test.xul', if you have test.xul file:///C:/testfiles/


    • If it doesn't work, move the test@test folder out of the extensions folder, close Firefox, start it up again, close it again, then move the test@test folder back into the extensions folder and start Firefox up again.



    Note, you should now also see the Test Extension in Tools->Add-ons. So basically, I've made a (very simple) extension by doing this.