Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         4chan Anonymouse File Names
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.2
  5. // @description  Changes your upload filenames to random timestamps and changes the hash of images
  6. // @author       Some Anon
  7. // @match        https://boards.4chan.org/*
  8. // @match        http://boards.4chan.org/*
  9. // @match        https://boards.4channel.org/*
  10. // @match        http://boards.4channel.org/*
  11. // @run-at      document-start
  12. // ==/UserScript==
  13.  
  14. //This script changes the input event of every type=file input field to an name-changing function
  15. //it can be used on every site where the onchange event isn't used for something else already
  16. //(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)
  17.  
  18. function createFileList(a) {
  19.     a = [].slice.call(Array.isArray(a) ? a : arguments)
  20.     for (var c, b = c = a.length, d = !0; b-- && d;) d = a[b] instanceof File
  21.     if (!d) throw new TypeError('expected argument to FileList is File or array of File objects')
  22.     for (b = (new ClipboardEvent('')).clipboardData || new DataTransfer; c--;) b.items.add(a[c])
  23.     return b.files
  24. }
  25.  
  26. function getFilename() {
  27.     var curtime = new Date().getTime();
  28.     return curtime - Math.floor(Math.random() * 365 * 24 * 60 * 60 * 1000);
  29. }
  30.  
  31. function fileNameChange() {
  32.     var element = this;
  33.     var mimetype = element.files[0].type;
  34.     var filename = getFilename() + '.' + element.files[0].name.split('.')[1];
  35.     //change name and write element first immediately because fast responding sites
  36.     //would not catch after hash change
  37.     var file = new File([element.files[0]], filename, {type: mimetype});
  38.     element.files = createFileList(file);
  39.     console.log("Change filename to " + filename);
  40.     if (mimetype == 'image/png' || mimetype == 'image/jpeg') {
  41.         var reader = new FileReader();
  42.         reader.addEventListener("load", function () {
  43.             var imgs = new Image();
  44.             imgs.src = reader.result;
  45.             imgs.onload = function() {
  46.                 var cvs = document.createElement('canvas');
  47.                 cvs.width = imgs.naturalWidth;
  48.                 cvs.height = imgs.naturalHeight;
  49.                 var canvas = cvs.getContext("2d");
  50.                 console.log("Change Imagehash");
  51.                 canvas.drawImage(imgs, 0, 0);
  52.                 canvas.fillStyle = "rgba(" + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + "," + Math.floor(Math.random() * 255) + ",255)";
  53.                 canvas.fillRect(Math.floor(Math.random() * cvs.width), Math.floor(Math.random() * cvs.height), 1, 1 );
  54.                 var newImageData = cvs.toBlob(function(blob) {
  55.                     file = new File([blob], filename, {type: mimetype});
  56.                     element.files = createFileList(file);
  57.                 }, 'image/jpeg', 0.9);
  58.             }
  59.         }, false)
  60.         reader.readAsDataURL(element.files[0]);
  61.     }
  62. }
  63.  
  64. function addNameChangeEvent(element) {
  65.     element.addEventListener('change', fileNameChange);
  66. }
  67.  
  68. function mutationChange (mutations) {
  69.   mutations.forEach((mutation) => {
  70.     var nodes = mutation.addedNodes;
  71.     for(var n = 0; n < nodes.length; n++) {
  72.         if(typeof nodes[n].type !== 'undefined' && nodes[n].type.toLowerCase() == 'file') {
  73.             //if element itself is input=file
  74.             addNameChangeEvent(nodes[n]);
  75.         }
  76.         else {
  77.             //search child nodes for input=file
  78.             var nodesl = nodes[n].getElementsByTagName("input");
  79.             for(var i=0; i<nodesl.length; i++) {
  80.                 if(nodesl[i].type.toLowerCase() == 'file') {
  81.                     addNameChangeEvent(nodesl[i]);
  82.                 }
  83.             }
  84.         }
  85.     }
  86.   });
  87. };
  88.  
  89. window.addEventListener('load', function() {
  90.     //add event to all already existing fileinput DOM elements
  91.     var inputs = document.getElementsByTagName('input');
  92.     for(var i = 0; i < inputs.length; i++) {
  93.         if(inputs[i].type.toLowerCase() == 'file') {
  94.             addNameChangeEvent(inputs[i]);
  95.         }
  96.     }
  97.     //check for later added fileinput DOM elements and add event
  98.     const observer = new MutationObserver(mutationChange);
  99.     observer.observe(document.body, {childList: true});
  100. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement