Source: popup.js

  1. /**
  2. * @file The javascript file of popup.html (which is what is displayed when clicking the plugin-icon).
  3. * Communicates with the injected code (content.js) via message passing.
  4. * @author Ulrik Schremser
  5. */
  6. $(document).ready(function() {
  7. // LISTENER FOR ENABLED-BUTTON
  8. $("#enabled-switch-label").click(function(event) {
  9. // If it is checked but now clicked, this means:
  10. // GOING INACTIVE
  11. if($("#enabled-switch").get(0).checked){
  12. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  13. chrome.tabs.sendMessage(tabs[0].id, {action: "disable"});
  14. });
  15. // Deactivating input elements
  16. disableUi();
  17. }
  18. // GOING ACTIVE
  19. else{
  20. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  21. chrome.tabs.sendMessage(tabs[0].id, {action: "enable"});
  22. });
  23. // Activating input elements
  24. enableUi();
  25. }
  26. });
  27. // LISTENER FOR TOOLTIP-BUTTON
  28. $("#tooltip-switch-label").click(function(event) {
  29. // Only react if we are active!
  30. if( ! $("#tooltip-switch").get(0).disabled){
  31. // If it is checked but now clicked, this means:
  32. // GOING INACTIVE
  33. if($("#tooltip-switch").get(0).checked){
  34. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  35. chrome.tabs.sendMessage(tabs[0].id, {action: "disable-tooltip"});
  36. });
  37. }
  38. // GOING ACTIVE
  39. else{
  40. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  41. chrome.tabs.sendMessage(tabs[0].id, {action: "enable-tooltip"});
  42. });
  43. }
  44. }
  45. });
  46. // ADDING LISTENERS TO SLIDERS
  47. $(".feature-slider").each(function(){
  48. $(this).on("input", function(){
  49. correctSliders('#' + this.id);
  50. });
  51. $(this).on("change", function(){
  52. updateScore();
  53. // Update the colors if they are set!
  54. if($("#enabled-switch").get(0).checked){
  55. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  56. chrome.tabs.sendMessage(tabs[0].id, {action: "enable"});
  57. });
  58. }
  59. });
  60. });
  61. });
  62. /**
  63. * Updates the score inside the injected content.js by sending a message to it containing the new values of the sliders.
  64. * @memberof popup.js
  65. */
  66. function updateScore(){
  67. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  68. chrome.tabs.sendMessage(tabs[0].id, {action: "update-score",
  69. sentenceLengthInfluence: $("#sentence-length-influence").get(0).value,
  70. wordLengthInfluence: $("#word-length-influence").get(0).value,
  71. ntvRatioInfluence: $("#ntv-ratio-influence").get(0).value,
  72. sentenceComplexityInfluence: $("#sentence-complexity-influence").get(0).value,
  73. wordComplexityInfluence: $("#word-complexity-influence").get(0).value
  74. });
  75. });
  76. }
  77. /**
  78. * Disables the UA components of the plugin
  79. * @memberof popup.js
  80. */
  81. function disableUi(){
  82. $(".feature-slider").each(function(){
  83. $(this).attr('disabled', 'disabled');
  84. });
  85. $("#tooltip-switch").attr('disabled', 'disabled');
  86. $('.inactive').each(function(){
  87. $(this).removeClass("hidden");
  88. });
  89. $('.active').each(function(){
  90. $(this).addClass('hidden');
  91. });
  92. }
  93. /**
  94. * Enables the UI components of the plugin
  95. * @memberof popup.js
  96. */
  97. function enableUi(){
  98. $("#tooltip-switch").removeAttr("disabled");
  99. $(".feature-slider").each(function(){
  100. $(this).removeAttr("disabled");
  101. });
  102. $('.inactive').each(function(){
  103. $(this).addClass('hidden');
  104. });
  105. $('.active').each(function(){
  106. $(this).removeClass("hidden");
  107. });
  108. }
  109. /**
  110. * Changes the values of the other slides to sum the max value
  111. * @param {String} idCurrent - The id of the slider that has been changed
  112. * @memberof popup.js
  113. */
  114. function correctSliders(idCurrent){
  115. var restShould = parseFloat($(idCurrent).get(0).max) - parseFloat($(idCurrent).get(0).value);
  116. // Use 1 to avoid division by 0.
  117. var sumRest = 0;
  118. $(".feature-slider").each(function(){
  119. if(('#' + this.id) != idCurrent){
  120. sumRest += parseFloat(this.value);
  121. }
  122. });
  123. // Should sum to restShould
  124. var scale = sumRest / restShould;
  125. $(".feature-slider").each(function(){
  126. if(('#' + this.id) != idCurrent){
  127. this.value /= scale;
  128. }
  129. });
  130. }
  131. // Load values from content script
  132. chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  133. chrome.tabs.sendMessage(tabs[0].id, {action: "get-values"}, function(vals){
  134. $("#enabled-switch").get(0).checked = vals[0];
  135. if(vals[0]){
  136. enableUi();
  137. }
  138. else{
  139. disableUi();
  140. }
  141. $("#tooltip-switch").get(0).checked = vals[1];
  142. $("#sentence-length-influence").get(0).value = vals[2];
  143. $("#word-length-influence").get(0).value = vals[3];
  144. $("#ntv-ratio-influence").get(0).value = vals[4];
  145. $("#sentence-complexity-influence").get(0).value = vals[5];
  146. $("#word-complexity-influence").get(0).value = vals[6];
  147. });
  148. });