Skip to content

Instantly share code, notes, and snippets.

@demeritcowboy
Created November 3, 2016 21:01
Show Gist options
  • Save demeritcowboy/3b3ce39f0543037b8b2fb5b775af4983 to your computer and use it in GitHub Desktop.
Save demeritcowboy/3b3ce39f0543037b8b2fb5b775af4983 to your computer and use it in GitHub Desktop.
// Useful for copying a webpage full of settings from e.g. staging to live for// sites that keep their config in the database and don't have a way// to set things via files.//// Because of browser security this is a two-step process. It doesn't// do the copying, instead it generates javascript code that will set the// fields to the same values.// - Paste the code into the javascript console on the page to be copied.// - Run the code.// - Copy the output in the console log to the code console on the other page.// - Run that.//// Note this doesn't trigger event bindings, so for example for settings pages// that dynamically add fields to forms as you go it won't work perfectly, but// if a field is already on the form just hidden then it will still work.(function($) {  var s = '';  $('input[type="text"], textarea, select').each(function() {    var t = $(this);    var v = t.val();    if (v != "") {      s += '$(' + JSON.stringify('#' + this.id) + ').val(' + JSON.stringify(v) + ');' + "\n";    }  });  $('input[type="radio"], input[type="checkbox"]').each(function() {    if (this.id != '') {      var v = 'false'; // note string not boolean      if (this.checked) {        v = 'true';      }      s += 'document.getElementById(' + JSON.stringify(this.id) + ').checked = ' + v + ';' + "\n";    }  });  console.log(s);})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment