mozilla

Compare Revisions

File I/O

Change Revisions

Revision 39495:

Revision 39495 by sdwilsh on

Revision 39496:

Revision 39496 by sdwilsh on

Title:
File I/O
File I/O
Slug:
Code_snippets/File_I//O
Code_snippets/File_I//O
Tags:
Extensions, Add-ons, Files, Streams, "Code snippets"
Extensions, Add-ons, Files, Streams, "Code snippets"
Content:

Revision 39495
Revision 39496
t202</pre>t
203    <h4 name="Getting_your_extension.27s_folder">
204      Getting your extension's folder
205    </h4>
206    <div class="note">
207      <strong>Note</strong>: This will no longer work in Gecko 2.
>0 / Firefox 4.0, because XPIs are no longer extracted during inst 
>allation, unless you specify &lt;em:unpack&gt; 
208    </div>
209    <h5 name="Via_nsIExtensionManager">
210      Via <code>nsIExtensionManager</code>
211    </h5>
212    <pre class="brush: js">
213// the extension's id from <a href="/en/Install_Manifests" title=
>"en/Install_Manifests">install.rdf</a> 
214var MY_ID = "<a class=" link-mailto" href="mailto:myextension@my.
>name" rel="freelink">myextension@my.name</a>"; 
215var em = Components.classes["@mozilla.org/extensions/manager;1"].
216         getService(Components.interfaces.nsIExtensionManager);
217// the path may use forward slash ("/") as the delimiter
218// returns nsIFile for the extension's install.rdf
219var file = em.getInstallLocation(MY_ID).getItemFile(MY_ID, "insta
>ll.rdf"); 
220var filestring = file.path;
221</pre>
222    <h5 name="From_an_XPCOM_component">
223      From an XPCOM component
224    </h5>
225    <p>
226      To get the directory that an extension is installed in, use
> an XPCOM component written in JavaScript. The special <code>__LO 
>CATION__</code> builtin will return an {{ interface("nsIFile") }} 
> of the component file: 
227    </p>
228    <p>
229      extensiondir/component/GetExtensionDirectory.js:
230    </p>
231    <pre class="brush: js">
232var componentFile = __LOCATION__;
233var componentsDir = componentFile.parent;
234var extensionDir = componentsDir.parent;

Back to History