A LILiK chrome extension for the well known 9gag website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
6.1 KiB

  1. var settings = [
  2. {
  3. id: "long_post_visualization_enabler",
  4. name: "Long Post Visualization Mod Enabler",
  5. description: "Enable long post mod?",
  6. type: "checkbox",
  7. },{
  8. id: "long_post_visualization",
  9. name: "Long Post Visualization",
  10. description: "Select which way you prefer to display long posts",
  11. type: "radio",
  12. options: [ "Sidebar" ]
  13. },{
  14. id: "night_mode_enabler",
  15. name: "Night Mode",
  16. description: "Enable or disable night mode",
  17. type: "checkbox"
  18. },{
  19. id: "night_mode_starting_hour",
  20. name: "Night Mode Starting Hour",
  21. description: "in hours. Ex: 19",
  22. type: "range",
  23. options: {
  24. min: "0",
  25. max: "24"
  26. }
  27. },{
  28. id: "night_mode_ending_hour",
  29. name: "Night Mode Ending Hour",
  30. description: "in hours. Ex: 7",
  31. type: "range",
  32. options: {
  33. min: "0",
  34. max: "24"
  35. }
  36. },{
  37. id: "video_controls_enabler",
  38. name: "Video controls",
  39. description: "Show controls on gif posts to pause or seeking gif?",
  40. type: "checkbox",
  41. },{
  42. id: "nsfw_enabler",
  43. name: "Show NSFW",
  44. description: "Show NSFW posts without login?",
  45. type: "checkbox",
  46. },{
  47. id: "disable_wakeup_enabler",
  48. name: "Wakeup pop-up",
  49. description: "Disable wakeup pop-up?",
  50. type: "checkbox",
  51. }
  52. ];
  53. // var settings = [
  54. // {
  55. // id: "1",
  56. // name: "Message 1",
  57. // description: "Lorem ipsum sit dolor amet",
  58. // type: "checkbox"
  59. // }, {
  60. // id: "2",
  61. // name: "Message 2",
  62. // description: "Sim secutur iusit",
  63. // type: "checkbox"
  64. // }, {
  65. // id: "3",
  66. // name: "Message 3",
  67. // description: "",
  68. // type: "radio",
  69. // options: [ "black", "blue"]
  70. // }, {
  71. // id: "4",
  72. // name: "Message 4",
  73. // description: "",
  74. // type: "range",
  75. // options: {
  76. // min: "0",
  77. // max: "100"
  78. // }
  79. // }, {
  80. // id: "5",
  81. // name: "Message 5",
  82. // description: "",
  83. // type: "dropdown",
  84. // options: [ "test", "seconda voce"]
  85. // }, {
  86. // id: "6",
  87. // name: "Message 6",
  88. // description: "",
  89. // type: "text"
  90. // },
  91. // ];
  92. document.addEventListener('DOMContentLoaded', function(){
  93. displaySettings();
  94. addListeners();
  95. loadSettings();
  96. });
  97. function displaySettings(){
  98. var tableSettings = document.getElementById("settings");
  99. var tableBody = tableSettings.getElementsByTagName("tbody");
  100. var newElement = "";
  101. settings.forEach( function( setting ){
  102. var type = "";
  103. newElement += "<tr id='setting_"+setting.id+"' data-type='"+setting.type+"' class='setting'><td>"+setting.name+"<span class='description'>"+setting.description+"</span></td><td>";
  104. switch ( setting.type ){
  105. case "checkbox":
  106. newElement += "<input id='checkbox_"+setting.id+"' type='"+setting.type+"'>";
  107. break;
  108. case "radio":
  109. setting.options.forEach( function( option ){
  110. newElement += "<input type='"+setting.type+"' name='radio_"+setting.id+"' id='radio_"+setting.id+"_"+option+"' value='"+option+"'>";
  111. newElement += "<label for='radio_"+setting.id+"_"+option+"' >"+option+"</label>";
  112. });
  113. break;
  114. case "range":
  115. newElement += "<input type='"+setting.type+"' class='rangeSelector' id='range_"+setting.id+"' min='"+setting.options.min+"' max='"+setting.options.max+"' >";
  116. newElement += "<label for='range_"+setting.id+"' id='range_value_"+setting.id+"' ></label>";
  117. break;
  118. case "text":
  119. newElement += "<input type='"+setting.type+"'>";
  120. break;
  121. case "dropdown":
  122. newElement += "<select>";
  123. setting.options.forEach( function( option ){
  124. newElement += "<option value='"+option.replace(" ", "_")+"'>"+option+"</option>";
  125. });
  126. newElement += "</select>";
  127. break;
  128. }
  129. newElement = newElement + "</td></tr>";
  130. });
  131. tableBody.innerHTML = newElement;
  132. tableSettings.innerHTML += tableBody.innerHTML;
  133. }
  134. function addListeners(){
  135. var settingsElement = document.getElementsByClassName("setting");
  136. for (var i = settingsElement.length - 1; i >= 0; i--) {
  137. settingsElement[i].addEventListener('change', saveSettings);
  138. }
  139. var rangeElements = document.getElementsByClassName("rangeSelector");
  140. for (var i = rangeElements.length - 1; i >= 0; i--) {
  141. rangeElements[i].addEventListener('change', function( element ){
  142. var labelIdNumber = this.id.split(/_(.+)?/)[1];
  143. updateRangeValueLabel( labelIdNumber, this.value);
  144. });
  145. };
  146. }
  147. //saves the data into storage
  148. function saveSettings(){
  149. var setting = this.id;
  150. setting = setting.split(/_(.+)?/)[1];
  151. var type = this.getAttribute('data-type');
  152. switch (type){
  153. case "checkbox":
  154. var value = this.childNodes[1].childNodes[0].checked;
  155. break;
  156. case "radio":
  157. var value = document.querySelector('input[name = "radio_'+setting+'"]:checked').value;
  158. break;
  159. case "text":
  160. case "range":
  161. case "dropdown":
  162. var value = this.childNodes[1].childNodes[0].value;
  163. break;
  164. }
  165. var obj = {};
  166. obj[setting] = value;
  167. chrome.storage.sync.set(
  168. obj
  169. , function(){
  170. //callback function after saving the preferences
  171. document.getElementById("bar").innerHTML = "Preferences saved, refresh 9gag pages to enable new preferences.";
  172. });
  173. }
  174. //get the value stored in the storage and will be shown in the text box.
  175. function loadSettings( ){
  176. chrome.storage.sync.get( null ,function(obj){
  177. updateSettings(obj);
  178. });
  179. }
  180. function updateSettings( storedSettings ){
  181. for( var settingId in storedSettings ){
  182. settingId = String( settingId );
  183. settingObject = document.getElementById( "setting_"+settingId );
  184. if( typeof settingObject != "undefined" && settingObject != null){
  185. type = settingObject.getAttribute('data-type');
  186. switch( type ){
  187. case ( "checkbox" ):
  188. settingObject.childNodes[1].childNodes[0].checked = storedSettings[settingId];
  189. break;
  190. case ( "radio" ):
  191. document.getElementById( "radio_"+ settingId +"_"+ storedSettings[settingId] ).checked = true;
  192. break;
  193. case "range":
  194. settingObject.childNodes[1].childNodes[0].value = storedSettings[settingId];
  195. updateRangeValueLabel( settingId , storedSettings[settingId] );
  196. break;
  197. case "text":
  198. case "dropdown":
  199. settingObject.childNodes[1].childNodes[0].value = storedSettings[settingId];
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. function updateRangeValueLabel( labelIdNumber, value ){// console.log(this.value);
  206. var label = document.getElementById("range_value_"+labelIdNumber);
  207. label.innerHTML = value;
  208. }