Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name 4chan Anonymouse File Names
- // @namespace http://tampermonkey.net/
- // @version 0.2
- // @description Changes your upload filenames to random timestamps and changes the hash of images
- // @author Some Anon
- // @match https://boards.4chan.org/*
- // @match http://boards.4chan.org/*
- // @match https://boards.4channel.org/*
- // @match http://boards.4channel.org/*
- // @run-at document-start
- // ==/UserScript==
- //This script changes the input event of every type=file input field to an name-changing function
- //it can be used on every site where the onchange event isn't used for something else already
- //(if it is, you can always also hook it up to the input event or make it call the original change event after name changing)
- function createFileList(a) {
- a = [].slice.call(Array.isArray(a) ? a : arguments)
- for (var c, b = c = a.length, d = !0; b-- && d;) d = a[b] instanceof File
- if (!d) throw new TypeError('expected argument to FileList is File or array of File objects')
- for (b = (new ClipboardEvent('')).clipboardData || new DataTransfer; c--;) b.items.add(a[c])
- return b.files
- }
- function getFilename() {
- var curtime = new Date().getTime();
- return curtime - Math.floor(Math.random() * 365 * 24 * 60 * 60 * 1000);
- }
- function fileNameChange() {
- var element = this;
- var mimetype = element.files[0].type;
- var filename = getFilename() + '.' + element.files[0].name.split('.')[1];
- //change name and write element first immediately because fast responding sites
- //would not catch after hash change
- var file = new File([element.files[0]], filename, {type: mimetype});
- element.files = createFileList(file);
- console.log("Change filename to " + filename);
- if (mimetype == 'image/png' || mimetype == 'image/jpeg') {
- var reader = new FileReader();
- reader.addEventListener("load", function () {
- var imgs = new Image();
- imgs.src = reader.result;
- imgs.onload = function() {
- var cvs = document.createElement('canvas');
- cvs.width = imgs.naturalWidth;
- cvs.height = imgs.naturalHeight;
- var canvas = cvs.getContext("2d");
- console.log("Change Imagehash");
- canvas.drawImage(imgs, 0, 0);
- canvas.fillStyle = "rgba(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ",255)";
- canvas.fillRect(Math.floor(Math.random() * cvs.width), Math.floor(Math.random() * cvs.height), 1, 1 );
- var newImageData = cvs.toBlob(function(blob) {
- file = new File([blob], filename, {type: mimetype});
- element.files = createFileList(file);
- }, 'image/jpeg', 0.9);
- }
- }, false)
- reader.readAsDataURL(element.files[0]);
- }
- }
- function addNameChangeEvent(element) {
- element.addEventListener('change', fileNameChange);
- }
- function mutationChange (mutations) {
- mutations.forEach((mutation) => {
- var nodes = mutation.addedNodes;
- for(var n = 0; n < nodes.length; n++) {
- if(typeof nodes[n].type !== 'undefined' && nodes[n].type.toLowerCase() == 'file') {
- //if element itself is input=file
- addNameChangeEvent(nodes[n]);
- }
- else {
- //search child nodes for input=file
- var nodesl = nodes[n].getElementsByTagName("input");
- for(var i=0; i<nodesl.length; i++) {
- if(nodesl[i].type.toLowerCase() == 'file') {
- addNameChangeEvent(nodesl[i]);
- }
- }
- }
- }
- });
- };
- window.addEventListener('load', function() {
- //add event to all already existing fileinput DOM elements
- var inputs = document.getElementsByTagName('input');
- for(var i = 0; i < inputs.length; i++) {
- if(inputs[i].type.toLowerCase() == 'file') {
- addNameChangeEvent(inputs[i]);
- }
- }
- //check for later added fileinput DOM elements and add event
- const observer = new MutationObserver(mutationChange);
- observer.observe(document.body, {childList: true});
- }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement