Skip to content

Commit

Permalink
Add an option for disabling/enabling animations only on some domains
Browse files Browse the repository at this point in the history
Closes #10.
  • Loading branch information
simonlindholm committed May 18, 2014
1 parent 946abe1 commit a22ee87
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
49 changes: 49 additions & 0 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ function startup(aData, aReason) {
updateHoverListeners(win, false);
} catch(ex) {}
});

maybeHookContentWindows();
unloaders.push(maybeHookContentWindows.bind(null, true));
}

function shutdown(aData, aReason) {
Expand Down Expand Up @@ -616,6 +619,48 @@ function watchWindows(callback, uncallback) {
});
}

var contentWindowObserver = null;
var exceptionList = [];
function maybeHookContentWindows(forceUnhook) {
function id(x) { return x; }
function addDot(x) { return "." + x; }
exceptionList = Prefs.pauseExceptions.split(/[ ,]+/).filter(id).map(addDot);
var shouldHook = exceptionList.length > 0 && !forceUnhook;
var hasObs = contentWindowObserver !== null;
if (shouldHook === hasObs)
return;
if (shouldHook) {
contentWindowObserver = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
observe: function contentDocumentObserve(doc, topic, data) {
if (topic === "document-element-inserted")
handleContentDocumentLoad(doc);
}
};
Services.obs.addObserver(contentWindowObserver, "document-element-inserted", false);
}
else {
Services.obs.removeObserver(contentWindowObserver, "document-element-inserted");
contentWindowObserver = null;
}
}

function handleContentDocumentLoad(doc) {
var win = doc.defaultView, host = doc.location && doc.location.host;
if (!host || !win || win.document !== doc)
return;
var list = exceptionList, loc = "." + host;
for (var i = 0; i < list.length; i++) {
if (loc.endsWith(list[i]))
break;
}
if (i === list.length) return;

// This site is on the exception list. Play gifs iff the default is to pause them.
var play = Prefs.defaultPaused;
setGifStateForWindow(win, play);
}

function initPrefs() {
var defaults = {
defaultPaused: false,
Expand All @@ -624,6 +669,7 @@ function initPrefs() {
enableShortcuts: true,
originalAnimationMode: "undefined",
playOnHover: false,
pauseExceptions: "",
};
var defaultBranch = Services.prefs.getDefaultBranch(PrefBranch);
var branch = Services.prefs.getBranch(PrefBranch);
Expand Down Expand Up @@ -695,6 +741,9 @@ function initPrefs() {
clearHoverEffect();
forAllWindows(updateHoverListeners);
}
else if (key === "pauseExceptions") {
maybeHookContentWindows();
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<em:targetApplication>
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>18.0</em:minVersion>
<em:minVersion>19.0</em:minVersion>
<em:maxVersion>30.0a1</em:maxVersion>
</Description>
</em:targetApplication>
Expand Down
1 change: 1 addition & 0 deletions options.xul
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
<setting type="bool" pref="extensions.togglegifs.toggleOnClick" title="Toggle GIFs on click" />
<setting type="bool" pref="extensions.togglegifs.enableShortcuts" title="Enable Ctrl+M and Shift+M shortcuts" />
<setting type="bool" pref="extensions.togglegifs.defaultPaused" title="Pause GIFs by default" />
<setting type="string" pref="extensions.togglegifs.pauseExceptions" title="– override the above setting for these sites (e.g. example.com, example.org)" />
<setting type="bool" pref="extensions.togglegifs.playOnHover" title="Auto-play on hover" />
</vbox>

0 comments on commit a22ee87

Please sign in to comment.