-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.contentMutations.js
More file actions
99 lines (77 loc) · 2.95 KB
/
jquery.contentMutations.js
File metadata and controls
99 lines (77 loc) · 2.95 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
;(function ( $ ) {
var pluginName = 'contentMutations',
defaults = {
attributes: false,
childList: true,
subtree: true,
fallbackTimer: 1000,
debounceTime: 50
};
function Plugin( element, options ) {
this.element = element;
this.$element = $(element);
this.options = $.extend( {}, defaults, options );
this._defaults = defaults;
this._name = pluginName;
this.init();
}
var debounce = function( func, wait, immediate ) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if ( !immediate ) func.apply( context, args );
};
var callNow = immediate && !timeout;
clearTimeout( timeout );
timeout = setTimeout( later, wait );
if ( callNow ) func.apply( context, args );
};
};
Plugin.prototype.init = function () {
this.MutationObserver = (function () {
var prefixes = [ 'WebKit', 'Moz', 'O', 'Ms', '' ];
for( var i=0; i < prefixes.length; i++ ) {
if( prefixes[i] + 'MutationObserver' in window ) {
return window[ prefixes[i] + 'MutationObserver' ];
}
}
return false;
}());
if(this.MutationObserver) {
this.observer = new this.MutationObserver($.proxy(function( mutations ) {
mutations.forEach( debounce($.proxy(function( mutation ) {
if ( typeof this.options.callback === 'function' ) {
this.options.callback.call();
}
}, this )), this.options.debounceTime );
}, this ));
this.observer.observe( this.element, this.options );
}
else {
setInterval( $.proxy(function() {
if ( typeof this.options.callback === 'function' ) {
this.options.callback.call();
}
}, this), this.options.fallbackTimer );
}
};
Plugin.prototype.destroy = function () {
this.observer.disconnect();
};
$.fn[ pluginName ] = function ( arg ) {
var args = arguments;
return this.each(function () {
var d = $.data( this, pluginName );
if ( !d && ( typeof arg === 'object' || typeof arg === 'undefined' )) {
$.data( this, pluginName,
new Plugin( this, arg ));
}
else if ( d && typeof arg === 'string' && typeof d[arg] === 'function' ) {
d[ arg ].apply( d, Array.prototype.slice.call( args, 1 ));
}
return this;
});
};
})( jQuery );