From 7b532fd83c62d6862dc03375ef717418e509b52a Mon Sep 17 00:00:00 2001 From: "Elf M. Sternberg" Date: Sat, 20 Feb 2016 11:56:28 -0800 Subject: [PATCH] [feat] Detects multiple instances. Re-detects every second. Warning moved. This allows the user to enjoy "infinite scroll" without worrying about future script injections. It moves the warning to next-to the "reblog" button, so you get a clearer picture of what you're up against. It also no longer interferes with the reblog iframe, so you still get full interactivity. --- tumblr-evil-detector.js | 47 +++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/tumblr-evil-detector.js b/tumblr-evil-detector.js index e00a77d..9ce0bf7 100644 --- a/tumblr-evil-detector.js +++ b/tumblr-evil-detector.js @@ -3,19 +3,52 @@ // @namespace http://elfsternberg.com // @version 0.1 // @description Detect if a Tumblr post has an embedded script in it -// @author Elf M. Sternberg +// @author You // @match http://*.tumblr.com/post/* // @grant none // ==/UserScript== /* jshint -W097 */ 'use strict'; -function tumblr_reblog_warning() { - var post = document.querySelectorAll('.post-wrapper script'); - if (post.length > 0) { - document.body.innerHTML += ('
' + - 'SCRIPT DETECTED
'); +if (!Element.prototype.matches) Element.prototype.matches = Element.prototype.msMatchesSelector; +if (!Element.prototype.closest) Element.prototype.closest = function(selector) { + var el = this; + while (el) { + if (el.matches(selector)) { + return el; + } + el = el.parentElement; } +}; + +function createFrag(htmlStr) { + var frag = document.createDocumentFragment(), + temp = document.createElement('div'); + temp.innerHTML = htmlStr; + while (temp.firstChild) { + frag.appendChild(temp.firstChild); + } + return frag; } -window.setTimeout(tumblr_reblog_warning, 500); +function tumblr_reblog_warning() { + var post = document.querySelectorAll('.post-wrapper script'); + var parent; + var already; + var panel; + for(var i = 0; i < post.length; i++) { + parent = post[i].closest('.post-wrapper'); + already = parent.querySelectorAll('.script-detected'); + if (already.length == 0) { + parent.style.border = "4px solid cyan"; + panel = parent.querySelector('.post-footer') + panel.insertBefore( + createFrag('
' + + 'SCRIPT DETECTED
'), + panel.childNodes[0]); + } + } + setTimeout(tumblr_reblog_warning, 1000); +} + +setTimeout(tumblr_reblog_warning, 500);