WaniKani Settings

try to take over the world!

当前为 2016-08-30 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/22751/144754/WaniKani%20Settings.js

  1. function noSpaces(value){
  2. return value.replace(/ /g,'');
  3. }
  4.  
  5. function makeSettings(Name, data){
  6. if($('.dropdown.account .dropdown-menu li.nav-header:contains("Scripts")').length === 0){
  7. $('.dropdown.account .dropdown-menu').append('<li id="scriptsMenu" class="nav-header">Scripts</li>');
  8. }
  9. $('#scriptsMenu').after("<li><a id='div" + noSpaces(Name) + "Link' href='#' onclick='$(\"#div" + noSpaces(Name) + "Settings\").dialog(\"open\");return false;'>" + Name + "</a></li>");
  10. $('head').append('<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css" type="text/css" />');
  11. if (typeof jQuery.ui == 'undefined') {
  12. jQuery.getScript("https://code.jquery.com/ui/1.12.0/jquery-ui.js", function(data2, status, jqxhr) {
  13. makeSettings2(Name, data);
  14. });
  15. } else {
  16. makeSettings2(Name, data);
  17. }
  18. }
  19.  
  20. function makeSettings2(Name, data){
  21. debugger;
  22. var divSettings = "<div id='div" + noSpaces(Name) + "Settings'><table>";
  23. $.each(data,function(item,value){
  24. divSettings = divSettings + '<tr><td><span>' + value.Display + '</span></td>';
  25. switch(value.Type) {
  26. case "textbox":
  27. divSettings = divSettings + '<td><input type="textbox" id="txt' + value.Name + '"></input></td></tr>';
  28. break;
  29. case "checkbox":
  30. divSettings = divSettings + '<td><input type="checkbox" id="chk' + value.Name + '"></input></td></tr>';
  31. break;
  32. case "select":
  33. divSettings = divSettings + '<td><select id="ddl' + value.Name + '">';
  34. $.each(value.Options,function(item2,value2){
  35. divSettings = divSettings + '<option value="' + value2.Value + '">' + value2.Text + '</option>';
  36. });
  37. divSettings = divSettings + '</td></tr>';
  38. break;
  39. default:
  40. break;
  41. }
  42. });
  43. divSettings = divSettings + '</table>';
  44. $('section.progression').after(divSettings);
  45. $.each(data,function(item,value){
  46. switch(value.Type) {
  47. case "textbox":
  48. $('#txt' + value.Name).val(getSetting(value.Name));
  49. break;
  50. case "checkbox":
  51. $('#chk' + value.Name).val();
  52. if(getSetting(value.Name) === "1"){
  53. $('#chk' + value.Name).attr('checked','checked');
  54. }
  55. break;
  56. case "select":
  57. $('#ddl' + value.Name).val(getSetting(value.Name));
  58. break;
  59. default:
  60. return;
  61. }
  62. });
  63. $('#div' + noSpaces(Name) + "Settings").dialog({
  64. autoOpen: false,
  65. height: 300,
  66. width: 400,
  67. modal: true,
  68. buttons: {
  69. "Save": function () {
  70. $.each(data,function(item,value){
  71. switch(value.Type) {
  72. case "textbox":
  73. localStorage.setItem(value.Name,$('#txt' + value.Name).val());
  74. break;
  75. case "checkbox":
  76. if($('#chk' + value.Name).attr('checked') == 'checked'){
  77. localStorage.setItem(value.Name,"1");
  78. } else {
  79. localStorage.setItem(value.Name,"0");
  80. }
  81. break;
  82. case "select":
  83. localStorage.setItem(value.Name,$('#ddl' + value.Name).val());
  84. break;
  85. default:
  86. return;
  87. }
  88. });
  89. $(this).dialog("close");
  90. },
  91. Cancel: function () {
  92. $(this).dialog("close");
  93. }
  94. }
  95. });
  96. }
  97.  
  98. function getSetting(setting){
  99. return localStorage.getItem(setting);
  100. }