-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimprint-next.js
More file actions
360 lines (314 loc) · 15.3 KB
/
imprint-next.js
File metadata and controls
360 lines (314 loc) · 15.3 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
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
// ==UserScript==
// @name Imprint-next
// @namespace http://tampermonkey.net/
// @version 0.1
// @description parser
// @author Alex
// @match https://*/*
// @grant none
// ==/UserScript==
(function(root, factory){
'use strict';
if (typeof define == 'function' && define.amd){
define(factory);
} else if (typeof module === 'object' && module.exports){
module.exports = factory(!0);
} else {
window.Parcel = factory();
window.parcel = new window.Parcel();
}
}(window, function(){
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.WorkerTimer=f()}})(function(){var define,module,exports;return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){(function(global){"use strict";if(global===global.window&&global.URL&&global.Blob&&global.Worker){module.exports=function(){var TIMER_WORKER_SOURCE=["var timerIds = {}, _ = {};","_.setInterval = function(args) {"," timerIds[args.timerId] = setInterval(function() { postMessage(args.timerId); }, args.delay);","};","_.clearInterval = function(args) {"," clearInterval(timerIds[args.timerId]);","};","_.setTimeout = function(args) {"," timerIds[args.timerId] = setTimeout(function() { postMessage(args.timerId); }, args.delay);","};","_.clearTimeout = function(args) {"," clearTimeout(timerIds[args.timerId]);","};","onmessage = function(e) { _[e.data.type](e.data) };"].join("");var _timerId=0;var _callbacks={};var _timer=new global.Worker(global.URL.createObjectURL(new global.Blob([TIMER_WORKER_SOURCE],{type:"text/javascript"})));_timer.onmessage=function(e){if(_callbacks[e.data]){_callbacks[e.data].callback.apply(null,_callbacks[e.data].params)}};return{setInterval:function(callback,delay){var params=Array.prototype.slice.call(arguments,2);_timerId+=1;_timer.postMessage({type:"setInterval",timerId:_timerId,delay:delay});_callbacks[_timerId]={callback:callback,params:params};return _timerId},setTimeout:function(callback,delay){var params=Array.prototype.slice.call(arguments,2);_timerId+=1;_timer.postMessage({type:"setTimeout",timerId:_timerId,delay:delay});_callbacks[_timerId]={callback:callback,params:params};return _timerId},clearInterval:function(timerId){_timer.postMessage({type:"clearInterval",timerId:timerId});_callbacks[timerId]=null},clearTimeout:function(timerId){_timer.postMessage({type:"clearTimeout",timerId:timerId});_callbacks[timerId]=null}}}()}else{module.exports=global}}).call(this,typeof global!=="undefined"?global:typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{}]},{},[1])(1)});
var Imprint = function(){
this.observer = new MutationObserver(this.processMutation.bind(this));
this.observer.observe(document.body, {
attributes: true,
childList: true,
characterData: true,
subtree : true,
attributeOldValue : true,
characterDataOldValue : true,
});
this.listeners = [];
this.waitings = [];
this.promises = 0;
if (false && Notification.permission != "denied" && Notification.permission != "granted"){
Notification.requestPermission();
}
};
Imprint.prototype = {
notify : function(message){
return new Notification(message);
},
clearWaitings : function(){
while(this.waitings.length > 0){
WorkerTimer.clearTimeout(this.waitings.pop().split("#")[0]);
}
},
serviceStampProps : [
"$children",
"$attribute",
"$text",
"$html",
"$type",
"$key",
"$count",
"$value",
"$format",
],
format : {
"trim" : function(string){
string = string.toString();
return string.trim()
},
"rm-html" : function(string){
string = string.toString();
return string.replace(/(<([^>]+)>)/ig, "");
},
"number" : function(string){
var matches = string.match(/[-]{0,1}[\d]*[\.]{0,1}[\d]+/g);
var result = 0;
if (matches !== null){ result = parseFloat(matches[0]); }
return result;
},
},
trimMutaationListeners : function(){
console.log("[IMPRINT]", "Trimming mutation listeners");
this.loop(this.listeners, (callbacks, mutationStamp, listeners)=>{
var _callbacks = [];
this.loop(callbacks, (callback)=>{
if (callbacks !== false){
_callbacks.push(callback);
}
});
listen[mutationStamp] = _callbacks;
});
},
trimWaiters : function(){
var waitings = [];
this.loop(this.waitings, (value)=>{
var date = Number(value.split("#")[1]);
if (date > +new Date()){
waitings.push(value);
}
});
this.waitings = waitings;
},
wait : function(callback, timeout){
var tID;
var wID = this.waitings.length;
tID = WorkerTimer.setTimeout(()=>{
this.trimWaiters();
callback();
}, timeout * 1000);
this.waitings.push([tID, (+new Date() + (timeout * 1000))].join("#"));;
},
processMutation : function(mutationData){
if (typeof mutationData.length == "number"){
this.loop(mutationData, (mutationData)=>{
this.processMutation(mutationData);
});
return;
}
if (window.parcelLogMutation){
console.log("[IMPRINT]", mutationData);
debugger;
}
this.loop(this.listeners, (callbacks, mutationStamp, list)=>{
let matched = this.validateMutation(mutationData, mutationStamp);
if (matched){
this.loop(callbacks, (callback, index)=>{
if (callback !== false){
callbacks[index] = false;
callback.call(this, callback, mutationStamp, mutationData);
}
});
} else {
}
});
},
validateMutation : function(mutationData, mutationStamp){
var result = true;
var mutationStampParced = mutationStamp.split("@");
this.loop(mutationStampParced, (token, key)=>{
if (this.evalMutationStampKey(
token,
mutationData.addedNodes,
mutationData.attributeName,
mutationData.attributeNamespace,
mutationData.nextSibling,
mutationData.oldValue,
mutationData.previousSibling,
mutationData.removedNodes,
mutationData.target,
mutationData.type
) !== true){
result = false;
return true;
}
});
return result;
},
evalMutationStampKey : function(
token,
addedNodes,
attributeName,
attributeNamespace,
nextSibling,
oldValue,
previousSibling,
removedNodes,
target,
type
){
var result = false;
try {
result = eval(token);
} catch(err){}
return result;
},
listen : function(mutationStamp, callback){
console.log("[IMPRINT]", "New Listener for " + mutationStamp);
this.listeners[mutationStamp] = this.listeners[mutationStamp] || [];
this.listeners[mutationStamp].push(callback);
},
dispatch : function(el, etype){
console.log("[IMPRINT]", "Dispatching " + etype + " event");
if (el.fireEvent) {
el.fireEvent('on' + etype);
} else {
var evObj = document.createEvent('Events');
evObj.initEvent(etype, true, false);
el.dispatchEvent(evObj);
}
},
applyFormats : function(data, format, stamp, source){
console.log("[IMPRINT]", "Applying formats");
if (typeof format == "object" && typeof format.length == "number"){
this.loop(format, (formatData) => {
data = this.applyFormats(data, formatData);
});
return data;
}
if (typeof format == "string" && typeof this.format[format] == "function"){
data = this.format[format](data, stamp, source);
}
if (typeof format == "function"){
data = format.call(this, data, stamp, source);
}
return data;
},
loop : function(list, cb, ctx){
if (!list){
return;
}
var _break = false;
if (typeof list == "object" && typeof list.length == "number"){
for (var a = 0, l = list.length; a < l; a++){
_break = cb.call(ctx, list[a], a, list);
if (_break){
break;
}
}
} else {
for (var a in list){
_break = cb.call(ctx, list[a], a, list);
if (_break){
break;
}
}
}
},
make : function(stamp, source){
console.log("[IMPRINT]", "Making node");
source = source || document.body;
if (typeof source == "object" && typeof source.length == "number"){
var outputData = [];
this.loop(source, (sourceItem, index) => {
var node = this.make(stamp, sourceItem);
if (node instanceof Promise){
let _index = index;
node.then((data)=>{
outputData[_index] = data;
});
} else {
outputData[index] = node;
}
});
return outputData;
}
var sourceData = null;
var outputData = {};
var isText = false;
var isContainer = true;
var stampsCount = 0;
switch(stamp.$type){
case "child":
if (source instanceof Node){
sourceData = source.querySelector(stamp.$key);
}
break;
case "children":
if (source instanceof Node){
sourceData = source.querySelectorAll(stamp.$key);
if (typeof stamp.$count == "number"){
sourceData = Array.prototype.slice.call(sourceData, 0, Math.min(sourceData.length, stamp.$count));
}
}
break;
case "attribute":
isText = true;
sourceData = source.getAttribute(stamp.$key);
break;
case "text":
isText = true;
sourceData = source.innerText;
break;
case "html":
isText = true;
sourceData = source.innerHTML;
break;
case "async":
if (typeof stamp.$key == "function"){
this.promises++;
sourceData = new Promise((resolve, reject)=>{
this.promises--;
stamp.$key.call(this, resolve, reject, stamp, source, stamp.$key);
});
}
break;
default:
sourceData = source;
break;
}
if (stamp.$value){
stampsCount++;
outputData = this.make(stamp.$value, sourceData);
} else {
this.loop(stamp, (childStamp, key) => {
if (this.serviceStampProps.indexOf(key) < 0){
stampsCount++;
node = null;
node = this.make(childStamp, sourceData);
if (node instanceof Promise){
let _key = key;
node.then((data)=>{
outputData[_key] = data;
});
} else {
outputData[key] = node;
}
}
});
}
if (stampsCount === 0){
outputData = sourceData;
}
if (stamp.$format){
outputData = this.applyFormats(outputData, stamp.$format, stamp, source);
}
return outputData;
}
};
return Imprint;
}));