addon-sdk/source/lib/sdk/content/mod.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/lib/sdk/content/mod.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,64 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +"use strict";
     1.8 +
     1.9 +module.metadata = {
    1.10 +  "stability": "experimental"
    1.11 +};
    1.12 +
    1.13 +const { Ci } = require("chrome");
    1.14 +const method = require("../../method/core");
    1.15 +const { add, remove, iterator } = require("../lang/weak-set");
    1.16 +
    1.17 +let getTargetWindow = method("getTargetWindow");
    1.18 +
    1.19 +getTargetWindow.define(function (target) {
    1.20 +  if (target instanceof Ci.nsIDOMWindow)
    1.21 +    return target;
    1.22 +  if (target instanceof Ci.nsIDOMDocument)
    1.23 +    return target.defaultView || null;
    1.24 +
    1.25 +  return null;
    1.26 +});
    1.27 +
    1.28 +exports.getTargetWindow = getTargetWindow;
    1.29 +
    1.30 +let attachTo = method("attachTo");
    1.31 +exports.attachTo = attachTo;
    1.32 +
    1.33 +let detachFrom = method("detatchFrom");
    1.34 +exports.detachFrom = detachFrom;
    1.35 +
    1.36 +function attach(modification, target) {
    1.37 +  if (!modification)
    1.38 +    return;
    1.39 +
    1.40 +  let window = getTargetWindow(target);
    1.41 +
    1.42 +  attachTo(modification, window);
    1.43 +
    1.44 +  // modification are stored per content; `window` reference can still be the
    1.45 +  // same even if the content is changed, therefore `document` is used instead.
    1.46 +  add(modification, window.document);
    1.47 +}
    1.48 +exports.attach = attach;
    1.49 +
    1.50 +function detach(modification, target) {
    1.51 +  if (!modification)
    1.52 +    return;
    1.53 +
    1.54 +  if (target) {
    1.55 +    let window = getTargetWindow(target);
    1.56 +    detachFrom(modification, window);
    1.57 +    remove(modification, window.document);
    1.58 +  }
    1.59 +  else {
    1.60 +    let documents = iterator(modification);
    1.61 +    for (let document of documents) {
    1.62 +      detachFrom(modification, document.defaultView);
    1.63 +      remove(modification, document);
    1.64 +    }
    1.65 +  }
    1.66 +}
    1.67 +exports.detach = detach;

mercurial