Created
May 26, 2016 10:49
-
-
Save r-k-b/896acc86ef3e7552396af3cf742c2f08 to your computer and use it in GitHub Desktop.
Fill input fields with dummy data
This file contains 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
// ==UserScript== | |
// @name Fill input fields with dummy data | |
// @namespace https://gist.github.com/r-k-b/ | |
// @version 1.0.0 | |
// @description Tired of repeatedly filling in form fields every time you update a form? Use this to fill in sensible dummy data. | |
// @author Robert K. Bell | |
// @homepage https://gist.github.com/r-k-b/896acc86ef3e7552396af3cf742c2f08 | |
// @downloadURL https://gist.github.com/r-k-b/896acc86ef3e7552396af3cf742c2f08/raw/fill_input_fields_with_dummy_data.user.js | |
// @include *://*/* | |
// @grant none | |
// @run-at context-menu | |
// ==/UserScript== | |
/* jshint esnext: true */ | |
(function($){ | |
var now = new Date(), | |
fillText = 'testing ' + now.toISOString(); | |
// TODO: select only visible input fields | |
$('input[type=text]').each(function(index, elem){ | |
// don't change input if it already has a val | |
if ($(elem).val().length > 0) { | |
return true; | |
} | |
var thisFillText = fillText; | |
// find a label | |
var $label = $("label[for='" + $(elem).attr('id') + "']"); | |
if ($label.length == 0) { | |
$label = $(elem).closest('label') | |
} | |
if ($label.length == 0) { | |
console.warn('No label found for ', elem); | |
} else { | |
// console.log('label found for', elem); | |
thisFillText = $label.text(); | |
} | |
thisFillText = thisFillText.replace('*', '').trim(); | |
// Give an email address to fields that are probably looking for valid email addresses | |
if (thisFillText.toLowerCase().indexOf('email') > -1) { | |
thisFillText = 'foo@bar.com' | |
} | |
$(elem).val(thisFillText).trigger('change'); | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment