-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtoggleGifAnimation.js
More file actions
131 lines (123 loc) · 4.47 KB
/
toggleGifAnimation.js
File metadata and controls
131 lines (123 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// http://infocatcher.ucoz.net/js/cb/toggleGifAnimation.js
// https://forum.mozilla-russia.org/viewtopic.php?id=57977
// https://github.com/Infocatcher/Custom_Buttons/tree/master/Toggle_GIF_Animation
// Toggle GIF Animation button for Custom Buttons
// (code for "initialization" section)
// (c) Infocatcher 2013-2020
// version 0.3.0 - 2020-02-25
// Tip: use image.animation_mode = none to disable animation by default
// http://kb.mozillazine.org/Firefox_:_Tips_:_Animated_Images#Image_animation_preference
var gifAnimation = this.gifAnimation = {
// Note: we use original button's icon to indicate disabled state
iconEnabled: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAABlElEQVR4nI2SsUsCcRTHv+/yJGzS4kCRWqqpQ5xahYwER/8CGxoPh2rxP6itWXLQKQiXGiQh3JrUKYgIogbvQBwMTTB/r8XfeXde5BcOfvd+733e9/d+P2JmAEC5XN7MZrN3RKRiJiKCVzKfmZ81TctRt14Ro9uLxUwfhXLnWEkeSYipaVoUrXSYX/O77NTbic7MzO/HO654Kx1my7LYsiw2TbPLzAgAwPb1C9qHkYWO/Y8etgDXnjyClOItSj707fX38MeOOePMbIMCmHVwJtiA0RQA0EisgRRCZCPoKgYA6tYrontpLDXE6NkVWD+whxiLxaIkaaVSaT+TyTwB8+v75xrNeDweDTg3hBCuIu/A/EAugPyWdLAIkA4k5C8HzrgvwFvsB1twIIRwAZxyAprNJgaDAQA0CoXC/CFJB0IITKdTe+39b7fb6HQ6N7VaLW87MAyDUqkUCSGYZu39BggAk8nkMZFInOq6HioWi0NiZlSr1XUiyqmqukdEEQCrAEIAgo5aAeBrPB6PFEXpAfgEcP8LWVgYRnUM0zMAAAAASUVORK5CYII=",
remote: window.gMultiProcessBrowser,
button: this,
getUtils: function(win) {
return win.windowUtils || win.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils);
},
get mode() {
var utils = this.getUtils(content);
return utils.imageAnimationMode;
},
set mode(mode) {
this.setMode(content, mode);
this.updateState(mode);
},
toggle: function() {
if(this.remote) {
this.toggleRemote();
return;
}
var ic = Components.interfaces.imgIContainer;
this.mode = this.mode == ic.kNormalAnimMode
? ic.kDontAnimMode
: ic.kNormalAnimMode;
},
toggleRemote: function() {
this.loadFrameScript(function() {
var ic = Components.interfaces.imgIContainer;
var mode = content.windowUtils.imageAnimationMode == ic.kNormalAnimMode ? ic.kDontAnimMode : ic.kNormalAnimMode;
(function setMode(win) {
try {
win.windowUtils.imageAnimationMode = mode;
}
catch(e) { // NS_ERROR_NOT_AVAILABLE
}
Array.prototype.forEach.call(win.frames, setMode);
})(content);
sendAsyncMessage("CB:ToggleGIFAnimation:mode", mode);
});
},
setMode: function(win, mode) {
Array.prototype.forEach.call(win.frames, function(win) {
this.setMode(win, mode);
}, this);
var utils = this.getUtils(win);
try {
utils.imageAnimationMode = mode;
}
catch(e) { // NS_ERROR_NOT_AVAILABLE
}
},
updateState: function(mode) {
if(mode === undefined && this.remote) {
this.updateStateRemote();
return;
}
if(mode === undefined)
mode = this.mode;
var btn = this.button;
var icon = btn.icon
|| btn.ownerDocument.getAnonymousElementByAttribute(btn, "class", "toolbarbutton-icon");
icon.src = mode == Components.interfaces.imgIContainer.kDontAnimMode
? btn.image
: this.iconEnabled;
},
updateStateRemote: function() {
this.loadFrameScript(function() {
try {
var mode = content.windowUtils.imageAnimationMode;
}
catch(e) { // NS_ERROR_NOT_AVAILABLE
}
sendAsyncMessage("CB:ToggleGIFAnimation:mode", mode);
});
},
loadFrameScript: function(fn) {
var code = "(" + fn + ")();";
var data = "data:application/javascript," + encodeURIComponent(code);
var mm = gBrowser.selectedBrowser.messageManager;
var _this = this;
mm.addMessageListener("CB:ToggleGIFAnimation:mode", function receiveMessage(msg) {
mm.removeMessageListener("CB:ToggleGIFAnimation:mode", receiveMessage);
if(msg.data != null)
_this.updateState(msg.data);
});
mm.loadFrameScript(data, false);
},
_updateStateTimer: 0,
updateStateDelayed: function() {
if(this._updateStateTimer)
return;
this._updateStateTimer = setTimeout(function(_this) {
_this._updateStateTimer = 0;
_this.updateState();
}, 20, this);
}
};
this.setAttribute("oncommand", "this.gifAnimation.toggle();");
var progressListener = {
onLocationChange: function(aWebProgress, aRequest, aLocation) {
gifAnimation.updateStateDelayed();
}
};
//progressListener.onStateChange =
// progressListener.onProgressChange =
// progressListener.onStatusChange =
// progressListener.onSecurityChange = function dummy() {};
gifAnimation.updateStateDelayed();
gBrowser.addProgressListener(progressListener);
this.onDestroy = function() {
gBrowser.removeProgressListener(progressListener);
};