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.

114 lines
3.6 KiB

7 years ago
  1. var muffcastUrl = "http://localhost:8128";
  2. browser.storage.local.get("muffcast").then(function(result) {
  3. muffcastUrl = result.muffcast && result.muffcast.url || muffcastUrl;
  4. })
  5. var clientUpdate = function() {
  6. return browser.tabs.query({
  7. currentWindow: true,
  8. active: true
  9. }).then(function(tabs) {
  10. var tab = tabs[0];
  11. return browser.tabs.sendMessage(tab.id, {
  12. command: "update"
  13. });
  14. });
  15. }
  16. var setStatus = function() {
  17. var xhttp = new XMLHttpRequest();
  18. xhttp.open("GET", muffcastUrl, true);
  19. xhttp.onreadystatechange = function() {
  20. var idleElement = document.getElementById("idle");
  21. var errorElement = document.getElementById("error");
  22. var muffcastElement = document.getElementById("muffcast");
  23. if (this.readyState == 4) {
  24. if (this.status == 200) {
  25. var status = this.responseText && JSON.parse(this.responseText);
  26. if (status && status.running) {
  27. var titleElement = document.getElementById("title");
  28. titleElement.textContent = status.title;
  29. titleElement.href = decodeURIComponent(status.url);
  30. var hostElement = document.getElementById("host");
  31. hostElement.textContent = decodeURIComponent(status.host);
  32. var muteElement = document.getElementById("mute");
  33. var updateMuteElement = function() {
  34. muteElement.innerHTML = status.muted ? '<i class="fa fa-fw fa-volume-off"></i>' : (status.volume < 0.5 ? '<i class="fa fa-fw fa-volume-down"></i>' : '<i class="fa fa-fw fa-volume-up"></i>');
  35. }
  36. muteElement.addEventListener("click", function(event) {
  37. status.muted = !status.muted;
  38. browser.runtime.sendMessage({
  39. "command": "mute",
  40. "muted": status.muted
  41. });
  42. volumeElement.value = status.muted ? 0 : status.volume;
  43. updateMuteElement();
  44. clientUpdate();
  45. })
  46. updateMuteElement();
  47. var volumeElement = document.getElementById("volume");
  48. volumeElement.setAttribute("value", status.muted ? 0 : status.volume);
  49. volumeElement.addEventListener("change", function(event) {
  50. browser.runtime.sendMessage({
  51. "command": "volume",
  52. "volume": volumeElement.value
  53. });
  54. status.volume = volumeElement.value;
  55. status.muted = volumeElement.value == 0;
  56. updateMuteElement();
  57. });
  58. var playElement = document.getElementById("play");
  59. playElement.innerHTML = status.playing ? '<i class="fa fa-fw fa-pause"></i>' : '<i class="fa fa-fw fa-play"></i>';
  60. playElement.addEventListener("click", function(event) {
  61. browser.runtime.sendMessage({
  62. "command": status.playing ? "pause" : "play"
  63. });
  64. status.playing = !status.playing;
  65. playElement.innerHTML = status.playing ? '<i class="fa fa-fw fa-pause"></i>' : '<i class="fa fa-fw fa-play"></i>';
  66. clientUpdate();
  67. })
  68. var stopElement = document.getElementById("stop");
  69. stopElement.addEventListener("click", function(event) {
  70. browser.runtime.sendMessage({
  71. "command": "stop"
  72. });
  73. clientUpdate();
  74. muffcastElement.classList.remove("visible");
  75. idleElement.classList.add("visible");
  76. errorElement.classList.remove("visible");
  77. })
  78. muffcastElement.classList.add("visible");
  79. idleElement.classList.remove("visible");
  80. errorElement.classList.remove("visible");
  81. } else {
  82. muffcastElement.classList.remove("visible");
  83. idleElement.classList.add("visible");
  84. errorElement.classList.remove("visible");
  85. }
  86. } else {
  87. muffcastElement.classList.remove("visible");
  88. idleElement.classList.remove("visible");
  89. errorElement.classList.add("visible");
  90. }
  91. }
  92. }
  93. xhttp.setRequestHeader("Content-type", "application/json");
  94. xhttp.send();
  95. }
  96. setStatus();