Gazelle : External CSS Saver

Save External CSS

  1. // ==UserScript==
  2. // @name Gazelle : External CSS Saver
  3. // @namespace hateradio)))
  4. // @description Save External CSS
  5. // @include http*://*.what.cd/user.php?action=edit&userid=*
  6. // @include http*://*.apollo.rip/user.php?action=edit&userid=*
  7. // @version 2
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function(){
  12. var strg, saver;
  13. strg = {
  14. on:(function(){ try { var s = window.localStorage; if(s.getItem&&s.setItem&&s.removeItem){return true;} }catch(e){return false;} }()),
  15. read:function(key){ return this.on ? JSON.parse(window.localStorage.getItem(key)) : false; },
  16. save:function(key, dat){ return this.on ? !window.localStorage.setItem(key, JSON.stringify(dat)) : false; },
  17. wipe:function(key){ return this.on ? !window.localStorage.removeItem(key) : false; },
  18. zero:function(o){ var k; for(k in o){ if(o.hasOwnProperty(k)){ return false; } } return true; },
  19. grab:function(key, def){ return this.read(key) || def; }
  20. };
  21. saver = {
  22. boxes : 8,
  23. cssBoxUrl : document.getElementById('styleurl'),
  24. cssBox : document.getElementById('styleurl').parentNode,
  25. submitButton : document.querySelector('input[value="Save Profile"]'),
  26. data : strg.grab('css_saves', {}),
  27. reg : /(?:\.css)/,
  28. init : function () {
  29. this.box.insert();
  30. this.submitButton.addEventListener('click', this.box.saveValues, false);
  31. },
  32. box : {
  33. saveValues : function () {
  34. var i = 0, s = {}, v = '';
  35. for (i; i < saver.boxes; i++) {
  36. v = document.getElementById('addcss'+i).value;
  37. s[i] = saver.reg.test(v) ? v : '';
  38. }
  39. strg.save('css_saves', s);
  40. },
  41. putValue : function () {
  42. if (saver.reg.test(this.value)) {
  43. document.getElementById('styleurl').value = this.value;
  44. }
  45. },
  46. getValue : function (id) {
  47. return saver.data[id] || '';
  48. },
  49. insert : function () {
  50. var i = 0, e, f = document.createDocumentFragment(), s = document.createElement('small');
  51. f.appendChild(document.createElement('br'));
  52. for (i; i < saver.boxes; i++) {
  53. e = document.createElement('input');
  54. e.type = 'url';
  55. e.id = 'addcss' + i;
  56. e.value = this.getValue(i);
  57. e.size = 60;
  58. e.addEventListener('dblclick', saver.box.putValue, false);
  59. f.appendChild(e);
  60. f.appendChild(document.createElement('br'));
  61. }
  62. s.textContent = 'Submit the form to save.';
  63. f.appendChild(s);
  64. saver.cssBox.appendChild(f);
  65. }
  66. }
  67. };
  68. saver.init();
  69. }());