options.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. Copyright 2015 Mozilla Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. "use strict";
  14. var storageAreaName = chrome.storage.sync ? "sync" : "local";
  15. var storageArea = chrome.storage[storageAreaName];
  16. Promise.all([
  17. new Promise(function getManagedPrefs(resolve) {
  18. if (!chrome.storage.managed) {
  19. resolve({});
  20. return;
  21. }
  22. // Get preferences as set by the system administrator.
  23. chrome.storage.managed.get(null, function (prefs) {
  24. // Managed storage may be disabled, e.g. in Opera.
  25. resolve(prefs || {});
  26. });
  27. }),
  28. new Promise(function getUserPrefs(resolve) {
  29. storageArea.get(null, function (prefs) {
  30. resolve(prefs || {});
  31. });
  32. }),
  33. new Promise(function getStorageSchema(resolve) {
  34. // Get the storage schema - a dictionary of preferences.
  35. var x = new XMLHttpRequest();
  36. var schema_location = chrome.runtime.getManifest().storage.managed_schema;
  37. x.open("get", chrome.runtime.getURL(schema_location));
  38. x.onload = function () {
  39. resolve(x.response.properties);
  40. };
  41. x.responseType = "json";
  42. x.send();
  43. }),
  44. ])
  45. .then(function (values) {
  46. var managedPrefs = values[0];
  47. var userPrefs = values[1];
  48. var schema = values[2];
  49. function getPrefValue(prefName) {
  50. if (prefName in userPrefs) {
  51. return userPrefs[prefName];
  52. } else if (prefName in managedPrefs) {
  53. return managedPrefs[prefName];
  54. }
  55. return schema[prefName].default;
  56. }
  57. var prefNames = Object.keys(schema);
  58. var renderPreferenceFunctions = {};
  59. // Render options
  60. prefNames.forEach(function (prefName) {
  61. var prefSchema = schema[prefName];
  62. if (!prefSchema.title) {
  63. // Don't show preferences if the title is missing.
  64. return;
  65. }
  66. // A DOM element with a method renderPreference.
  67. var renderPreference;
  68. if (prefSchema.type === "boolean") {
  69. // Most prefs are booleans, render them in a generic way.
  70. renderPreference = renderBooleanPref(
  71. prefSchema.title,
  72. prefSchema.description,
  73. prefName
  74. );
  75. } else if (prefSchema.type === "integer" && prefSchema.enum) {
  76. // Most other prefs are integer-valued enumerations, render them in a
  77. // generic way too.
  78. // Unlike the renderBooleanPref branch, each preference handled by this
  79. // branch still needs its own template in options.html with
  80. // id="$prefName-template".
  81. renderPreference = renderEnumPref(prefSchema.title, prefName);
  82. } else if (prefName === "defaultZoomValue") {
  83. renderPreference = renderDefaultZoomValue(prefSchema.title);
  84. } else {
  85. // Should NEVER be reached. Only happens if a new type of preference is
  86. // added to the storage manifest.
  87. console.error("Don't know how to handle " + prefName + "!");
  88. return;
  89. }
  90. renderPreference(getPrefValue(prefName));
  91. renderPreferenceFunctions[prefName] = renderPreference;
  92. });
  93. // Names of preferences that are displayed in the UI.
  94. var renderedPrefNames = Object.keys(renderPreferenceFunctions);
  95. // Reset button to restore default settings.
  96. document.getElementById("reset-button").onclick = function () {
  97. userPrefs = {};
  98. storageArea.remove(prefNames, function () {
  99. renderedPrefNames.forEach(function (prefName) {
  100. renderPreferenceFunctions[prefName](getPrefValue(prefName));
  101. });
  102. });
  103. };
  104. // Automatically update the UI when the preferences were changed elsewhere.
  105. chrome.storage.onChanged.addListener(function (changes, areaName) {
  106. var prefs = null;
  107. if (areaName === storageAreaName) {
  108. prefs = userPrefs;
  109. } else if (areaName === "managed") {
  110. prefs = managedPrefs;
  111. }
  112. if (prefs) {
  113. renderedPrefNames.forEach(function (prefName) {
  114. var prefChanges = changes[prefName];
  115. if (prefChanges) {
  116. if ("newValue" in prefChanges) {
  117. userPrefs[prefName] = prefChanges.newValue;
  118. } else {
  119. // Otherwise the pref was deleted
  120. delete userPrefs[prefName];
  121. }
  122. renderPreferenceFunctions[prefName](getPrefValue(prefName));
  123. }
  124. });
  125. }
  126. });
  127. })
  128. .then(null, console.error.bind(console));
  129. function importTemplate(id) {
  130. return document.importNode(document.getElementById(id).content, true);
  131. }
  132. // Helpers to create UI elements that display the preference, and return a
  133. // function which updates the UI with the preference.
  134. function renderBooleanPref(shortDescription, description, prefName) {
  135. var wrapper = importTemplate("checkbox-template");
  136. wrapper.title = description;
  137. var checkbox = wrapper.querySelector('input[type="checkbox"]');
  138. checkbox.onchange = function () {
  139. var pref = {};
  140. pref[prefName] = this.checked;
  141. storageArea.set(pref);
  142. };
  143. wrapper.querySelector("span").textContent = shortDescription;
  144. document.getElementById("settings-boxes").append(wrapper);
  145. function renderPreference(value) {
  146. checkbox.checked = value;
  147. }
  148. return renderPreference;
  149. }
  150. function renderEnumPref(shortDescription, prefName) {
  151. var wrapper = importTemplate(prefName + "-template");
  152. var select = wrapper.querySelector("select");
  153. select.onchange = function () {
  154. var pref = {};
  155. pref[prefName] = parseInt(this.value);
  156. storageArea.set(pref);
  157. };
  158. wrapper.querySelector("span").textContent = shortDescription;
  159. document.getElementById("settings-boxes").append(wrapper);
  160. function renderPreference(value) {
  161. select.value = value;
  162. }
  163. return renderPreference;
  164. }
  165. function renderDefaultZoomValue(shortDescription) {
  166. var wrapper = importTemplate("defaultZoomValue-template");
  167. var select = wrapper.querySelector("select");
  168. select.onchange = function () {
  169. storageArea.set({
  170. defaultZoomValue: this.value,
  171. });
  172. };
  173. wrapper.querySelector("span").textContent = shortDescription;
  174. document.getElementById("settings-boxes").append(wrapper);
  175. function renderPreference(value) {
  176. value = value || "auto";
  177. select.value = value;
  178. var customOption = select.querySelector("option.custom-zoom");
  179. if (select.selectedIndex === -1 && value) {
  180. // Custom zoom percentage, e.g. set via managed preferences.
  181. // [zoom] or [zoom],[left],[top]
  182. customOption.text = value.indexOf(",") > 0 ? value : value + "%";
  183. customOption.value = value;
  184. customOption.hidden = false;
  185. customOption.selected = true;
  186. } else {
  187. customOption.hidden = true;
  188. }
  189. }
  190. return renderPreference;
  191. }