Advertisement
Guest User

Untitled

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