Skip to content

Instantly share code, notes, and snippets.

@captainbrosset
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save captainbrosset/388004c287ca93ae8358 to your computer and use it in GitHub Desktop.
Save captainbrosset/388004c287ca93ae8358 to your computer and use it in GitHub Desktop.
nsIDOMWindowUtils

nsIDOMWindowUtils provides some pretty interesting helpers. Here are some example chrome-privileged scripts.

// Get the 'distance' between 2 values of a CSS property
let win = gBrowser.mCurrentTab.linkedBrowser.contentWindow;
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
let element = win.document.querySelector(".dot");
utils.computeAnimationDistance(element, "width", "10px", "11px");
// Disable the automatic refresh driver and control time manually
let win = gBrowser.mCurrentTab.linkedBrowser.contentWindow;
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
utils.advanceTimeAndRefresh(0); // stop
utils.restoreNormalRefresh(); // restart
utils.advanceTimeAndRefresh(1000 / 60) // forward 1 tick
utils.advanceTimeAndRefresh(-1000 / 60) // backward 1 tick
// Get the framerate of paints on the current page, for a given amount of time
let {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
let win = gBrowser.mCurrentTab.linkedBrowser.contentWindow;
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
function wait(ms) {
let def = promise.defer();
win.setTimeout(def.resolve, ms);
return def.promise;
}
// YOU HAVE 5 SECONDS TO PLAY WITH THE PAGE, AFTER THAT DELAY, THE FRAMERATE WILL BE DISPLAYED IN THE CONSOLE
let time = 5000;
Task.spawn(function*() {
let recordingHandle = utils.startFrameTimeRecording();
yield wait(time);
let intervals = utils.stopFrameTimeRecording(recordingHandle);
let rate = intervals.length / (time/1000);
win.console.log(rate);
});
// Set the resolution of the page (this doesn't reflow the page in any way)
let {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
let {Promise: promise} = Cu.import("resource://gre/modules/Promise.jsm", {});
let win = gBrowser.mCurrentTab.linkedBrowser.contentWindow;
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
function wait(ms) {
let def = promise.defer();
win.setTimeout(def.resolve, ms);
return def.promise;
}
Task.spawn(function*() {
for (let i = 1; i > 0; i -= .1) {
utils.setResolution(i, i);
yield wait(50);
}
});
// Stop/Start all animated images in the window
let win = gBrowser.mCurrentTab.linkedBrowser.contentWindow;
let utils = win.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
utils.imageAnimationMode = 0 // stop
utils.imageAnimationMode = 1 // start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment