diff --git a/4chan_x.user.js b/4chan_x.user.js index 155db3dff..1a590ea26 100644 --- a/4chan_x.user.js +++ b/4chan_x.user.js @@ -527,29 +527,90 @@ return Array.prototype.slice.call(root.querySelectorAll(selector)); }; filter = { + regexps: {}, init: function() { - var filter, filters, key, m, regx, _i, _len; + var filter, key, m, regx, _i, _len, _results; HTMLBlockquoteElement.prototype.toString = function() { return ($.el('a', { innerHTML: this.innerHTML.replace(/
/g, '\n') })).textContent; }; - filters = {}; + _results = []; for (key in config.filter) { if (!(m = conf[key].match(/(.+)/g))) { continue; } - filters[key] = []; + this.regexps[key] = []; for (_i = 0, _len = m.length; _i < _len; _i++) { filter = m[_i]; try { if ((regx = eval(filter)).constructor === RegExp) { - filters[key].push(regx); + this.regexps[key].push(regx); } } catch (_e) {} } + _results.push(this.regexps[key].length ? g.callbacks.push(this[key]) : void 0); } - return log(filters); + return _results; + }, + test: function(key, value) { + var regexp, _i, _len, _ref; + _ref = filter.regexps[key]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + regexp = _ref[_i]; + if (regexp.test(value)) { + return true; + } + } + }, + name: function(root) { + var name; + if (!(name = root.className === 'op' ? $('.postername', root).textContent : $('.commentpostername', root).textContent)) { + return; + } + return filter.test('name', name); + }, + trip: function(root) { + var trip; + if (!(trip = $('.postertrip', root))) { + return; + } + return filter.test('trip', trip.textContent); + }, + mail: function(root) { + var mail; + if (!(mail = $('.linkmail', root))) { + return; + } + return filter.test('mail', mail.href); + }, + sub: function(root) { + var sub; + if (!(sub = root.className === 'op' ? $('.filetitle', root).textContent : $('.replytitle', root).textContent)) { + return; + } + return filter.test('sub', sub); + }, + com: function(root) { + var com; + if (!(com = $('blockquote', root).toString())) { + return; + } + return filter.test('com', com); + }, + file: function(root) { + var file; + if (!(file = $('.filesize span', root))) { + return; + } + return filter.test('file', file.title); + }, + md5: function(root) { + var img; + if (!(img = $('img[md5]', root))) { + return; + } + return filter.test('md5', img.getAttribute('md5')); } }; expandComment = { diff --git a/script.coffee b/script.coffee index 57fd1d2c3..a01f9e74d 100644 --- a/script.coffee +++ b/script.coffee @@ -384,20 +384,53 @@ $$ = (selector, root=d.body) -> Array::slice.call root.querySelectorAll selector filter = + regexps: {} init: -> HTMLBlockquoteElement.prototype.toString = -> return ($.el 'a', innerHTML: @innerHTML.replace /
/g, '\n').textContent - filters = {} for key of config.filter unless m = conf[key].match /(.+)/g continue - filters[key] = [] + @regexps[key] = [] for filter in m try if (regx = eval filter).constructor is RegExp - filters[key].push regx + @regexps[key].push regx + #only execute what's filterable + g.callbacks.push @[key] if @regexps[key].length - log filters + test: (key, value) -> + for regexp in filter.regexps[key] + return true if regexp.test value + + name: (root) -> + unless (name = if root.className is 'op' then $('.postername', root).textContent else $('.commentpostername', root).textContent) + return + filter.test 'name', name + trip: (root) -> + unless trip = $('.postertrip', root) + return + filter.test 'trip', trip.textContent + mail: (root) -> + unless mail = $('.linkmail', root) + return + filter.test 'mail', mail.href + sub: (root) -> + unless(sub = if root.className is 'op' then $('.filetitle', root).textContent else $('.replytitle', root).textContent) + return + filter.test 'sub', sub + com: (root) -> + unless com = $('blockquote', root).toString() + return + filter.test 'com', com + file: (root) -> + unless file = $ '.filesize span', root + return + filter.test 'file', file.title + md5: (root) -> + unless img = $ 'img[md5]', root + return + filter.test 'md5', img.getAttribute('md5') expandComment = init: ->