-
Notifications
You must be signed in to change notification settings - Fork 0
/
oven.user.iced
386 lines (335 loc) · 11 KB
/
oven.user.iced
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
###
Oven, a Google+ Userscript Framework
Copyright (C) 2013 Jingqin Lynn
Includes IcedCoffeeScript Compiler v1.6.2a
http://iced-coffee-script.github.io/iced-coffee-script
Copyright 2011, Jeremy Ashkenas, Maxwell Krohn
Released under the MIT License
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
###
###
// ==UserScript==
// @name Google+ Userscript Framework
// @namespace http://project.quietmusic.org/2012/userscript/oven/
// @description Google+ Userscript Framework
// @include *
// @grant GM_xmlhttpRequest
// @run-at document-start
// ==/UserScript==
###
return unless location.href.indexOf('https://plus.google.com/') == 0
if not window.ExtOvenEval
update_code = window.localStorage['ExtOvenCode']
if update_code?
window.ExtOvenEval = true
eval '//# sourceURL=OvenCode.user.js\n' + update_code
return
else
if window['chrome']? and not window.chrome['extension']?
# Userscripts in Chrome cannot access iframes.
# However, Chrome extensions can.
on_frame_loaded = (e) ->
return unless e.target.src.indexOf('https://plus.google.com/') == 0
return if e.target.contentWindow.ExtOvenEval
e.target.contentWindow.ExtOvenEval = true
e.target.contentWindow.eval(window.localStorage['ExtOvenCode'])
MutationObserver = window.MutationObserver ? window.WebKitMutationObserver
if !MutationObserver and unsafeWindow?
MutationObserver = (unsafeWindow.MutationObserver ?
unsafeWindow.WebKitMutationObserver)
if MutationObserver
observer = new MutationObserver (mutations) ->
mutations.forEach (mutation) ->
if mutation.type == 'childList' and mutation.addedNodes
for el in mutation.addedNodes
if el.tagName == 'IFRAME'
el.addEventListener 'load', on_frame_loaded, false
if el.querySelector and (result = el.querySelectorAll('iframe'))
for ell in result
ell.addEventListener 'load', on_frame_loaded, false
observer.observe document,
childList: true
subtree: true
class Oven
constructor: ->
@api =
lang: {}
manager: this
@snippets = {}
@status = {}
@storage = window.localStorage
@sync_interval = 0
@version = 1
@updateUrl = (@storage['ExtOvenUpdateUrl'] ?
'https://github.com/quietlynn/oven/raw/master/oven.user.js')
load: (callback) ->
snip = @storage['ExtOvenSnippets']
if not snip
@init =>
@load(callback)
return false
try
for name in JSON.parse @storage['ExtOvenSnippets']
data = JSON.parse @storage['ExtOvenSnippet_' + name]
@snippets[name] = data
catch
@panic()
@init => @load(callback)
return false
if @version > @storage['ExtOvenVersion']
@update =>
@load(callback)
callback() if callback
return true
version_compare: (v1, v2) ->
sv1 = v1.split "."
sv2 = v2.split "."
for i in [0..(sv1.length - 1)]
return 1 if sv2.length - 1 < i
i1 = parseInt sv1[i]
i2 = parseInt sv2[i]
return 1 if i1 > i2
return -1 if i1 < i2
return 0 if sv1.length == sv2.length
return -1
init: (callback) ->
@storage['ExtOvenUpdateDate'] = new Date()
@storage['ExtOvenVersion'] = @version
@snippets =
'oven.lang.coffee':
deps: ['oven.lang.iced']
code: 'oven.lang.coffee = oven.lang.iced;'
url: null
builtin: true
'oven.lang.iced':
deps: ['io.github.maxtaco.iced.compiler']
code: '''
oven.lang.iced = function (source) {
return CoffeeScript.compile(source, {runtime: 'window'});
}
'''
url: null
builtin: true
await @install(
'io.github.maxtaco.iced.compiler',
'https://maxtaco.github.io/coffee-script/extras/iced-coffee-script-1.8.0-a.js',
defer(data), 'bypass_cache')
if data
await @install(
'oven.ui',
'https://github.com/quietlynn/oven-gplus/raw/master/oven_ui.oven.iced',
defer(data), 'bypass_cache')
if data
for name of @snippets
@snippets[name].builtin = true
@save()
callback() if callback
panic: ->
for name of @storage
delete @storage[name] if name.indexOf('ExtOven') == 0
update: (callback) ->
# Update storage format...
@storage['ExtOvenVersion'] = @version
# Update Compiler URL...
@snippets['io.github.maxtaco.iced.compiler'].url =
'https://maxtaco.github.io/coffee-script/extras/iced-coffee-script-1.8.0-a.js'
callback()
save: ->
console.log 'Oven::save'
names = []
for name, data of @snippets
@storage['ExtOvenSnippet_' + name] = JSON.stringify data
names.push name
@storage['ExtOvenSnippets'] = JSON.stringify names
xhr: (url, callback, bypass_cache=false) ->
if bypass_cache
url += (if (/\?/).test(url) then "&" else "?") +
("_=" + (new Date()).getTime())
onreadystatechange = (xhr) ->
xhr = xhr.target if xhr.target
if xhr.readyState == 4
if callback
if xhr.status == 200
callback xhr.responseText
else
console.log "OVEN::xhr ERROR #{xhr.status}: #{xhr.responseText}"
callback null
if GM_xmlhttpRequest?
return GM_xmlhttpRequest(
method: 'GET'
url: url
onreadystatechange: onreadystatechange
)
else
xhr =
if window.XMLHttpRequest
new XMLHttpRequest
else if window.ActiveXObject
try
new ActiveXObject 'Msxml2.XMLHTTP'
catch _
try
new ActiveXObject 'Microsoft.XMLHTTP'
catch _
null
return null if not xhr
xhr.onreadystatechange = onreadystatechange
xhr.open 'GET', url, true
xhr.send()
return xhr
grab: (name, url, callback, bypass_cache=false) ->
await @xhr url, defer(code), bypass_cache
if not code?
callback(null) if callback
return
data = @parse name, url, code
callback data if callback
install: (name, url, callback, bypass_cache=false) ->
await @grab name, url, defer(data), bypass_cache
if data
data.name = name if name
if data.missing?
await @install_all data.missing, defer(), bypass_cache
delete data.missing
@add data.name, data
console.log 'Oven::install ' + data.name
callback data if callback
install_all: (deps, callback, bypass_cache=false) ->
await
for dep_name, dep_url of deps
@install dep_name, dep_url, defer(), bypass_cache
callback() if callback
parse: (name, url, code) ->
data = {}
data.deps = []
data.opts = []
reg = /(?:\n|^)\s*OVEN::(\w+)\s+(.*)/g
while result = reg.exec(code)
[_, field, value] = result
if field == 'require'
[dep, dep_url] = value.split /\s+/
data.deps.push dep
if not @installed(dep)
data.missing ?= {}
data.missing[dep] = dep_url
else if field == 'optional'
[dep, dep_url] = value.split /\s+/
data.opts.push dep
else if ['deps', 'opts', 'builtin', 'disabled'].indexOf(field) < 0
data[field] = value
data.url = url
data.last_update = new Date
data.version ?= 0
data.name ?= name ? url
data.display ?= data.name
if data.lang
@run 'oven.lang.' + data.lang
try
data.code = oven.api.lang[data.lang](code)
catch ex
console.log "Error compiling #{data.name} with #{data.lang}:"
console.log ex.toString()
else
data.code = code
return data
sync: (callback, bypass_cache=false) ->
console.log 'Oven::sync'
now = new Date()
await
if now - new Date(@storage['ExtOvenUpdateDate']) > @sync_interval
if @updateUrl
((autocb) =>
await @xhr @updateUrl, defer(code),
bypass_cache or not @storage['ExtOvenCode']
if code?
@storage['ExtOvenCode'] = code
@storage['ExtOvenUpdateDate'] = now
)(defer())
for name, data of @snippets
if data.url != null and not @snippets[name].disabled
if now - new Date(data.last_update) > @sync_interval
((autocb, name, builtin) =>
await @install name, data.url, defer(), bypass_cache
@snippets[name].builtin = builtin
)(defer(), name, data.builtin)
else
@snippets[name].last_update = now
@save()
callback() if callback
cook: ->
# Run language handlers first.
for name of @snippets
if name.indexOf 'oven.lang.' == 0
@run name
for name of @snippets
@run name
run: (name) ->
if @status[name] != 'loaded' and not @snippets[name].disabled
data = @snippets[name]
for dep in data.deps
@run dep
if data.opts
for opt in data.opts
@run opt if @installed(opt)
console.log 'Oven::run ' + name
@execute @snippets[name].code, name
@status[name] = 'loaded'
execute: (code, name='OVEN.execute') ->
fn = eval """
//# sourceURL=#{name}.oven.js
(function (oven) { try {
#{code}
} catch (ex) { console.log(ex.toString()); } })
"""
fn(@api)
installed: (name) -> @snippets[name]?
has: (name) -> @status[name] == 'loaded'
add: (name, data) ->
@snippets[name] = data
data.last_update = new Date()
remove: (name) ->
delete @snippets[name]
snips = []
for snip, data of @snippets
if data.deps.indexOf(name) >= 0
snips.push snip
for snip in snips
@remove snip
enable: (name) ->
data = @snippets[name]
for dep in data.deps
@enable dep
if data.disabled
console.log 'OVEN::enable ' + name
delete data.disabled
disable: (name) ->
console.log 'OVEN::disable ' + name
@snippets[name].disabled = true
for snip, data of @snippets
if data.deps.indexOf(name) >= 0
@disable snip
oven = new Oven
window.ExtOvenManager = oven
await
d = defer()
onready = ->
if document.querySelector('base')?.href.match(/^https:\/\/plus\.google\.com(\/u\/\d+)?\/?/)
d()
if document.readyState != 'loading'
onready()
else
document.addEventListener 'DOMContentLoaded', onready
oven.load defer()
oven.cook()
if window.self == window.top
window.addEventListener 'load', ->
oven.sync(null, 'bypass_cache')