File: manage.html

package info (click to toggle)
stylish 1.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,156 kB
  • ctags: 334
  • sloc: makefile: 19
file content (254 lines) | stat: -rw-r--r-- 8,457 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<!DOCTYPE HTML>
<html>
<head>
	<title>Stylish Manage</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link href="../skin/32.png" rel="icon">
	<style>
		table {
			border-spacing: 0;
		}
		#styles td {
			border-top: 1px solid #BBB;
		}
		#styles tr:last-child td {
			border-bottom: 1px solid #BBB;
		}
		.style-item td {
			padding-top: 0.5em;
			padding-bottom: 0.5em;
		}
		.style-item .enable-disable-column, .style-item .remove-column, .style-item .enable-disable-column, .style-item .update-column {
			text-align: center;
		}
		.style-item .enable-disable-column button {
			margin-left: 1em;
			margin-right: 1em;
		}
		.style-enabled .enable-style {
			display: none;
		}
		.style-disabled .disable-style {
			display: none;
		}
		.style-disabled .style-name {
			color: gray;
		}
		#check-all-for-updates-container {
			display: none;
		}
		.style-update-status, .checking-for-update-message, .update-style, .updating-message, .updated-message, .no-update-message, .update-error-message, .update-not-possible-message {
			display: none;
		}
		.style-update-status, .checking-for-update-message,  .updating-message, .updated-message, .no-update-message, .update-error-message, .update-not-possible-message {
			color: gray;
			font-size: smaller;
			font-style: italic;
			text-align: right;
		}
		.checking-for-update .checking-for-update-message, .update-error .update-error-message, .no-update .no-update-message, .updatable .update-style, .update-not-possible .update-not-possible-message {
			display: inline;
		}
		.checking-for-update .style-update-status, .update-error .style-update-status, .no-update .style-update-status, .update-not-possible .style-update-status {
			display: block;
		}
		.loading-image {
			display: none;
			margin-right: 0.5em;
		}
		.loading .loading-image {
			display: inline;
		}
	</style>
	<script src="common.js"></script>
</head>
<body>
<h1><img src="../skin/32.png" alt=""> Stylish</h1>

<table>
	<tbody id="styles">
	</tbody>
</table>

<p id="check-all-for-updates-container"><button id="check-all-for-updates" onclick="checkAllForUpdates()"><img src="chrome://browser/skin/tabbrowser/connecting.png" alt="" class="loading-image">Check all for updates</button></p>

<script>
	// None of the actions we can perform from this page will directly change the UI. Instead, we will listen to the observer service so we can change ourselves not only from actions from this page, but actions from anywhere.

	function escapeHtml(s) {
		return s
			.replace(/&/g, "&amp;")
			.replace(/'/g, "&apos;")
			.replace(/"/g, "&quot;")
			.replace(/>/g, "&gt;")
			.replace(/</g, "&lt;")
	}

	function get(id) {
		return service.find(id, service.REGISTER_STYLE_ON_CHANGE);
	}

	function getUI(id) {
		return document.getElementById("style-" + id);
	}

	function getNodeForStyle(id) {
		var frag = document.createDocumentFragment();
		frag.appendChild(document.createElement("tbody"));
		frag.firstChild.innerHTML = getCode(get(id));
		return frag.firstChild.firstChild;
	}

	function refreshUI(id) {
		var oldUi = getUI(id);
		oldUi.parentNode.replaceChild(getNodeForStyle(id), oldUi);
	}

	function enable(id) {
		var style = get(id);
		style.enabled = true;
		style.save();
	}

	function disable(id) {
		var style = get(id);
		style.enabled = false;
		style.save();
	}

	function removeStyle(id) {
		var style = get(id);
		style.delete();
	}

	function removeUI(id) {
		var e = getUI(id);
		e.parentNode.removeChild(e);
	}

	function addUI(id) {
		document.getElementById("styles").appendChild(getNodeForStyle(id));
	}

	function getCode(style) {
		return styleTemplate
			.replace(/STYLE_STATUS/g, style.enabled ? 'enabled' : 'disabled')
			.replace(/STYLE_ID/g, style.id)
			.replace(/STYLE_NAME/g, escapeHtml(style.name))
			.replace(/STYLE_LINK/g, style.url == null ? "" : "<a href=\"" + escapeHtml(style.url) + "\">[→]</a>");
	}

	function startInstallFromUrls(button) {
		var startedCallback = function() {
			updateButtonStatus(button, true);
		}
		var endedCallback = function() {
			updateButtonStatus(button, false);
		}
		stylishCommon.startInstallFromUrls(startedCallback, endedCallback);
	}

	var styleTemplate = "<tr id='style-STYLE_ID' class='style-item style-STYLE_STATUS'><td class='style-info'><div class='style-name'>STYLE_NAME STYLE_LINK</div><div class='style-update-status'><span class='checking-for-update-message'>Checking for updates...</span><span class='updated-message'>Updated.</span><span class='no-update-message'>No update available.</span><span class='update-error-message'>Error updating.</span><span class='update-not-possible-message'>No update possible.</span></div></td><td class='update-column'><button class='update-style' onclick='applyUpdate(\"STYLE_ID\")'><img src='chrome://browser/skin/tabbrowser/connecting.png' alt='' class='loading-image'>Update</button></td><td class='enable-disable-column'><button class='enable-style' onclick='enable(\"STYLE_ID\")'>Enable</button><button class='disable-style' onclick='disable(\"STYLE_ID\")'>Disable</button></td><td class='remove-column'><button class='remove-style' onclick='removeStyle(\"STYLE_ID\")'>Remove</button></td></tr>";
	var service = Components.classes["@userstyles.org/style;1"].getService(Components.interfaces.stylishStyle);
	var styles = service.list(0, {});
	var html = styles.map(getCode).join("\n");
	document.getElementById("styles").innerHTML = html;
	updateUpdateAllButton(styles.length > 0);
	
	function updateUpdateAllButton(show) {
		document.getElementById("check-all-for-updates-container").style.display = show ? "block" : "none";
	}

	var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);

	var deleteObserver = {
		observe: function(subject, topic, data) {
			removeUI(subject.id);
			updateUpdateAllButton(document.getElementById("styles").childNodes.length > 0);
		}
	};
	observerService.addObserver(deleteObserver, "stylish-style-delete", false);

	var addObserver = {
		observe: function(subject, topic, data) {
			addUI(subject.id);
			updateUpdateAllButton(true);
		}
	};
	observerService.addObserver(addObserver, "stylish-style-add", false);

	var updateObserver = {
		observe: function(subject, topic, data) {
			refreshUI(subject.id);
		}
	};
	observerService.addObserver(updateObserver, "stylish-style-change", false);
	
	var checkForUpdateObserver = {
		observe: function(subject, topic, data) {
			var ui = getUI(subject.id);
			if (topic == "stylish-style-update-check-start") {
				ui.classList.add("checking-for-update");
			} else if (topic == "stylish-style-update-check-done") {
				ui.classList.remove("checking-for-update");
				switch (data) {
					case "update-check-error":
						ui.classList.add("update-error");
						break;
					case "no-update-available":
						ui.classList.add("no-update");
						break;
					case "update-available":
						ui.classList.add("updatable");
						break;
					case "no-update-possible":
						ui.classList.add("update-not-possible");
						break;
				}
			}
		}
	};
	
	function checkAllForUpdates() {
		var button = document.getElementById("check-all-for-updates");
		updateButtonStatus(button, true);
		var styles = service.list(0, {});
		styles.forEach(function(style) {
			// remove any previous update stuff
			var cl = getUI(style.id).classList;
			cl.remove("update-error");
			cl.remove("no-update");
			cl.remove("updatable");
			cl.remove("update-not-possible");
			cl.remove("checking-for-update");

			style.checkForUpdates(checkForUpdateObserver);
		});
		updateButtonStatus(button, false);
	}
	
	function applyUpdate(id) {
		var ui = getUI(id);
		var button = ui.querySelector('.update-style');
		updateButtonStatus(button, true);
		get(id).applyUpdate(null);
		// that should fire stylish-style-change when done, which we're listening to above, and refresh it
	}
	
	function updateButtonStatus(button, loading) {
		if (loading) {
			button.classList.add("loading");
			button.setAttribute("disabled", "true");
		} else {
			button.classList.remove("loading");
			button.removeAttribute("disabled");
		}
	}

</script>

<p>Visit <a href="http://userstyles.org">userstyles.org</a> to install styles, or <button onclick="startInstallFromUrls(this)"><img src="chrome://browser/skin/tabbrowser/connecting.png" alt="" class="loading-image">install from URLs.</button></p>

</body>
</html>