Last active
May 4, 2016 10:16
-
-
Save kittsville/7eb3317988256ce9ec6b3e088be0f20e to your computer and use it in GitHub Desktop.
Allows bulk adding of subreddit user flair templates from a text file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Bulk Flair Upload | |
| * Version 0.1 | |
| * Author: @kittsville | |
| * Allows bulk adding of user flair templates from a text file | |
| * Only supports flair name, not CSS class | |
| */ | |
| if (!window.FileReader || ![].forEach) { | |
| throw "Your browser sucks so this won't work"; | |
| } | |
| var uploadButton = $('<button/>', {'text':'Upload'}), | |
| uploadField = $('<input/>', { | |
| 'type' : 'file', | |
| 'accept' : '.txt,.csv', | |
| 'style' : 'visibility:hidden;', | |
| }), | |
| uploadWrap = $('<div/>'), | |
| flairs; | |
| uploadButton.click(function() { | |
| uploadField.click(); | |
| }); | |
| uploadField.on('change', function(uploadEvent) { | |
| var files = uploadEvent.target.files; | |
| for (var i = 0; i < files.length; i++) { | |
| var file = files[i], | |
| reader = new FileReader(); | |
| reader.onload = function(e) { | |
| try { | |
| flairs = e.target.result.split(/\r\n|\n/); | |
| setFlairs(); | |
| } catch (error) { | |
| alert('Failed to parsed an uploaded file'); | |
| console.log(error); | |
| return; | |
| } | |
| }; | |
| reader.readAsText(file); | |
| } | |
| }); | |
| var setFlairs = function() { | |
| var newFlair = $('#empty-user-flair-template'), | |
| flairInput = newFlair.find('input[type="text"]:first'), | |
| submitButton = newFlair.find('button[type="submit"]'), | |
| flair = flairs.shift(); | |
| flairInput.val(flair); | |
| submitButton.click(); | |
| if (flairs.length > 0) { | |
| setTimeout(setFlairs, 1500); | |
| } | |
| }; | |
| uploadWrap.append(uploadButton); | |
| uploadWrap.append(uploadField); | |
| uploadWrap.insertBefore('#tabbedpane-templates'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment