WebExtension as alternative to Chromecast
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.

107 lines
3.2 KiB

7 years ago
  1. /*
  2. browser.tabs.query({
  3. currentWindow: true,
  4. active: true
  5. }).then(function(tabs) {
  6. var tab = tabs[0];
  7. browser.tabs.insertCSS(tab.id, {
  8. code: "@font-face { font-family: 'duospace';" +
  9. "src: url('" + browser.extension.getURL("fonts/iAWriterDuospace-Regular.eot") + "');" +
  10. "src: url('" + browser.extension.getURL("fonts/iAWriterDuospace-Regular.eot") + "?#iefix&v=4.7.0') format('embedded-opentype')," +
  11. "url('" + browser.extension.getURL("fonts/iAWriterDuospace-Regular.woff") + "') format('woff')," +
  12. "url('" + browser.extension.getURL("fonts/iAWriterDuospace-Regular.ttf") + "') format('truetype')," +
  13. "url('" + browser.extension.getURL("fonts/iAWriterDuospace-Regular.svg") + "') format('svg');" +
  14. "font - weight: normal;" +
  15. "font - style: normal;}"
  16. });
  17. })
  18. */
  19. var interval = 8000;
  20. var unsplash = {};
  21. browser.storage.local.get("muffcast").then(function(result) {
  22. interval = result.muffcast && result.muffcast.unsplashInterval || interval;
  23. unsplash.clientId = result.muffcast && result.muffcast.unsplashClient;
  24. unsplash.credit = result.muffcast && result.muffcast.unsplashCredit;
  25. setInterval(getUnsplash, interval);
  26. })
  27. var splash = {
  28. url: "",
  29. user: "",
  30. userUrl: ""
  31. }
  32. var getUnsplashRandom = function() {
  33. var xhttp = new XMLHttpRequest();
  34. xhttp.open("GET", "https://source.unsplash.com/random/1600x900", true);
  35. xhttp.addEventListener("load", function() {
  36. if (this.readyState == 4) {
  37. splash.url = this.responseURL;
  38. delete splash.description;
  39. delete splash.user;
  40. delete splash.userUrl;
  41. setBackground();
  42. }
  43. });
  44. xhttp.send();
  45. }
  46. var getUnsplash = function() {
  47. var background = document.getElementById("background");
  48. background.style.opacity = 0;
  49. if (unsplash && unsplash.clientId && unsplash.credit) {
  50. var xhttp = new XMLHttpRequest();
  51. xhttp.open("GET", "https://api.unsplash.com/photos/random?w=1600&h=900&client_id=" + unsplash.clientId, true);
  52. xhttp.addEventListener("load", function() {
  53. if (this.readyState == 4) {
  54. if (this.status == 200) {
  55. var response = this.responseText ? JSON.parse(this.responseText) : false;
  56. if (response) {
  57. splash.url = response.urls.custom;
  58. splash.description = response.description;
  59. splash.user = response.user.name;
  60. splash.userUrl = response.user.links.html + "?utm_source=" + unsplash.credit + "&utm_medium=referral&utm_campaign=api-credit";
  61. setBackground();
  62. }
  63. } else {
  64. getUnsplashRandom();
  65. }
  66. }
  67. });
  68. xhttp.setRequestHeader("content-type", "application/json");
  69. xhttp.send();
  70. } else {
  71. getUnsplashRandom();
  72. }
  73. }
  74. var setBackground = function() {
  75. setTimeout(function() {
  76. var background = document.getElementById("background");
  77. background.style['background-image'] = "url('" + splash.url + "')";
  78. background.style.opacity = 1;
  79. var userMeta = document.getElementById("user-meta");
  80. if (splash.user) {
  81. userMeta.classList.remove("hidden");
  82. var user = document.getElementById("user");
  83. user.href = splash.userUrl;
  84. user.innerHTML = splash.user;
  85. } else {
  86. userMeta.classList.add("hidden");
  87. }
  88. var description = document.getElementById("description");
  89. if (splash.description) {
  90. description.innerHTML = splash.description;
  91. } else {
  92. description.innerHTML = "";
  93. }
  94. }, 1500)
  95. }
  96. getUnsplash();