|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 "use strict"; |
|
6 |
|
7 module.metadata = { |
|
8 "stability": "experimental" |
|
9 }; |
|
10 |
|
11 const { Cc, Ci } = require("chrome"); |
|
12 |
|
13 const io = Cc['@mozilla.org/network/io-service;1']. |
|
14 getService(Ci.nsIIOService); |
|
15 |
|
16 const SHEET_TYPE = { |
|
17 "agent": "AGENT_SHEET", |
|
18 "user": "USER_SHEET", |
|
19 "author": "AUTHOR_SHEET" |
|
20 }; |
|
21 |
|
22 function getDOMWindowUtils(window) { |
|
23 return window.QueryInterface(Ci.nsIInterfaceRequestor). |
|
24 getInterface(Ci.nsIDOMWindowUtils); |
|
25 }; |
|
26 |
|
27 /** |
|
28 * Synchronously loads a style sheet from `uri` and adds it to the list of |
|
29 * additional style sheets of the document. |
|
30 * The sheets added takes effect immediately, and only on the document of the |
|
31 * `window` given. |
|
32 */ |
|
33 function loadSheet(window, url, type) { |
|
34 if (!(type && type in SHEET_TYPE)) |
|
35 type = "author"; |
|
36 |
|
37 type = SHEET_TYPE[type]; |
|
38 |
|
39 if (!(url instanceof Ci.nsIURI)) |
|
40 url = io.newURI(url, null, null); |
|
41 |
|
42 let winUtils = getDOMWindowUtils(window); |
|
43 try { |
|
44 winUtils.loadSheet(url, winUtils[type]); |
|
45 } |
|
46 catch (e) {}; |
|
47 }; |
|
48 exports.loadSheet = loadSheet; |
|
49 |
|
50 /** |
|
51 * Remove the document style sheet at `sheetURI` from the list of additional |
|
52 * style sheets of the document. The removal takes effect immediately. |
|
53 */ |
|
54 function removeSheet(window, url, type) { |
|
55 if (!(type && type in SHEET_TYPE)) |
|
56 type = "author"; |
|
57 |
|
58 type = SHEET_TYPE[type]; |
|
59 |
|
60 if (!(url instanceof Ci.nsIURI)) |
|
61 url = io.newURI(url, null, null); |
|
62 |
|
63 let winUtils = getDOMWindowUtils(window); |
|
64 |
|
65 try { |
|
66 winUtils.removeSheet(url, winUtils[type]); |
|
67 } |
|
68 catch (e) {}; |
|
69 }; |
|
70 exports.removeSheet = removeSheet; |
|
71 |
|
72 /** |
|
73 * Returns `true` if the `type` given is valid, otherwise `false`. |
|
74 * The values currently accepted are: "agent", "user" and "author". |
|
75 */ |
|
76 function isTypeValid(type) { |
|
77 return type in SHEET_TYPE; |
|
78 } |
|
79 exports.isTypeValid = isTypeValid; |