[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.
This commit is contained in:
Elf M. Sternberg 2016-02-20 11:56:28 -08:00
parent faac34ba93
commit 7b532fd83c
1 changed files with 40 additions and 7 deletions

View File

@ -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 <elf.sternberg@gmail.com>
// @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 += ('<div style="background-color: rgba(0,0,0,0.6); float: right; position: absolute; top: 60px; width: 320px; color:limegreen; border: 4px solid limegreen; z-index: 50">' +
'SCRIPT DETECTED</div>');
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('<div class="script-detected" style="background-color: rgba(0,0,0,0.6); float: left; color:limegreen; border: 4px solid limegreen; z-index: 50">' +
'SCRIPT DETECTED</div>'),
panel.childNodes[0]);
}
}
setTimeout(tumblr_reblog_warning, 1000);
}
setTimeout(tumblr_reblog_warning, 500);