storage.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /*!
  2. * Copyright 2018 The Rust Project Developers. See the COPYRIGHT
  3. * file at the top-level directory of this distribution and at
  4. * http://rust-lang.org/COPYRIGHT.
  5. *
  6. * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  7. * http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  8. * <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  9. * option. This file may not be copied, modified, or distributed
  10. * except according to those terms.
  11. */
  12. var currentTheme = document.getElementById("themeStyle");
  13. var mainTheme = document.getElementById("mainThemeStyle");
  14. function updateLocalStorage(name, value) {
  15. if (typeof(Storage) !== "undefined") {
  16. localStorage[name] = value;
  17. } else {
  18. // No Web Storage support so we do nothing
  19. }
  20. }
  21. function getCurrentValue(name) {
  22. if (typeof(Storage) !== "undefined" && localStorage[name] !== undefined) {
  23. return localStorage[name];
  24. }
  25. return null;
  26. }
  27. function switchTheme(styleElem, mainStyleElem, newTheme) {
  28. styleElem.href = mainStyleElem.href.replace("rustdoc.css", newTheme + ".css");
  29. updateLocalStorage('theme', newTheme);
  30. }
  31. switchTheme(currentTheme, mainTheme, getCurrentValue('theme') || 'main');