michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: module.metadata = { michael@0: "stability": "experimental" michael@0: }; michael@0: michael@0: const { Cc, Ci } = require("chrome"); michael@0: michael@0: const io = Cc['@mozilla.org/network/io-service;1']. michael@0: getService(Ci.nsIIOService); michael@0: michael@0: const SHEET_TYPE = { michael@0: "agent": "AGENT_SHEET", michael@0: "user": "USER_SHEET", michael@0: "author": "AUTHOR_SHEET" michael@0: }; michael@0: michael@0: function getDOMWindowUtils(window) { michael@0: return window.QueryInterface(Ci.nsIInterfaceRequestor). michael@0: getInterface(Ci.nsIDOMWindowUtils); michael@0: }; michael@0: michael@0: /** michael@0: * Synchronously loads a style sheet from `uri` and adds it to the list of michael@0: * additional style sheets of the document. michael@0: * The sheets added takes effect immediately, and only on the document of the michael@0: * `window` given. michael@0: */ michael@0: function loadSheet(window, url, type) { michael@0: if (!(type && type in SHEET_TYPE)) michael@0: type = "author"; michael@0: michael@0: type = SHEET_TYPE[type]; michael@0: michael@0: if (!(url instanceof Ci.nsIURI)) michael@0: url = io.newURI(url, null, null); michael@0: michael@0: let winUtils = getDOMWindowUtils(window); michael@0: try { michael@0: winUtils.loadSheet(url, winUtils[type]); michael@0: } michael@0: catch (e) {}; michael@0: }; michael@0: exports.loadSheet = loadSheet; michael@0: michael@0: /** michael@0: * Remove the document style sheet at `sheetURI` from the list of additional michael@0: * style sheets of the document. The removal takes effect immediately. michael@0: */ michael@0: function removeSheet(window, url, type) { michael@0: if (!(type && type in SHEET_TYPE)) michael@0: type = "author"; michael@0: michael@0: type = SHEET_TYPE[type]; michael@0: michael@0: if (!(url instanceof Ci.nsIURI)) michael@0: url = io.newURI(url, null, null); michael@0: michael@0: let winUtils = getDOMWindowUtils(window); michael@0: michael@0: try { michael@0: winUtils.removeSheet(url, winUtils[type]); michael@0: } michael@0: catch (e) {}; michael@0: }; michael@0: exports.removeSheet = removeSheet; michael@0: michael@0: /** michael@0: * Returns `true` if the `type` given is valid, otherwise `false`. michael@0: * The values currently accepted are: "agent", "user" and "author". michael@0: */ michael@0: function isTypeValid(type) { michael@0: return type in SHEET_TYPE; michael@0: } michael@0: exports.isTypeValid = isTypeValid;