aw.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import Toast from "toasters";
  2. import { generateUuid } from "@common/utils/generateUuid";
  3. let gotPong = false;
  4. let pingTries = 0;
  5. let uuid = null;
  6. let enabled = false;
  7. let lastTimeSentMediaDate = 0;
  8. let lastTimeDenied = 0;
  9. let lastTimeCompetitor = 0;
  10. const notConnectedToast = new Toast({
  11. content: "ActivityWatch is not connected yet.",
  12. persistent: true,
  13. interactable: false
  14. });
  15. notConnectedToast.hide();
  16. const sendingMediaDataToast = new Toast({
  17. content: "Sending media data to ActivityWatch.",
  18. persistent: true,
  19. interactable: false
  20. });
  21. sendingMediaDataToast.hide();
  22. const deniedToast = new Toast({
  23. content:
  24. "Another Musare instance is already sending data to ActivityWatch Musare extension. Please only use 1 active tab for stations and editsong.",
  25. persistent: true,
  26. interactable: false
  27. });
  28. deniedToast.hide();
  29. const competitorToast = new Toast({
  30. content:
  31. "Another Musare instance is already sending data to ActivityWatch Musare extension. Please only use 1 active tab for stations and editsong.",
  32. persistent: true,
  33. interactable: false
  34. });
  35. competitorToast.hide();
  36. export default {
  37. sendMediaData(mediaData) {
  38. if (enabled) {
  39. if (!gotPong) return false;
  40. if (lastTimeDenied + 1000 > Date.now()) return false;
  41. if (lastTimeCompetitor + 1000 > Date.now()) return false;
  42. lastTimeSentMediaDate = Date.now();
  43. this.sendEvent("videoData", mediaData);
  44. }
  45. return true;
  46. },
  47. sendEvent(type, data) {
  48. if (enabled)
  49. document.dispatchEvent(
  50. new CustomEvent("ActivityWatchMusareEvent", {
  51. detail: {
  52. type,
  53. source: uuid,
  54. data
  55. }
  56. })
  57. );
  58. },
  59. enable() {
  60. if (!enabled) {
  61. uuid = generateUuid();
  62. document.addEventListener(
  63. "ActivityWatchMusareEvent",
  64. this.eventListener
  65. );
  66. notConnectedToast.show();
  67. this.attemptPing();
  68. enabled = true;
  69. console.log("Enabled AW.");
  70. setInterval(() => {
  71. if (lastTimeDenied + 1000 > Date.now()) {
  72. deniedToast.show();
  73. } else {
  74. deniedToast.hide();
  75. }
  76. if (lastTimeCompetitor + 1000 > Date.now()) {
  77. competitorToast.show();
  78. } else {
  79. competitorToast.hide();
  80. }
  81. if (
  82. !(lastTimeDenied + 1000 > Date.now()) &&
  83. !(lastTimeCompetitor + 1000 > Date.now()) &&
  84. lastTimeSentMediaDate + 1000 > Date.now()
  85. ) {
  86. sendingMediaDataToast.show();
  87. } else {
  88. sendingMediaDataToast.hide();
  89. }
  90. }, 1000);
  91. }
  92. },
  93. disable() {
  94. document.removeEventListener(
  95. "ActivityWatchMusareEvent",
  96. this.eventListener
  97. );
  98. enabled = false;
  99. notConnectedToast.hide();
  100. console.log("Disabled AW.");
  101. },
  102. eventListener(event) {
  103. const data = event.detail;
  104. if (data.type === "pong") {
  105. gotPong = true;
  106. notConnectedToast.hide();
  107. new Toast("Got pong, connected to ActivityWatch Musare extension");
  108. }
  109. if (data.type === "denied") {
  110. lastTimeDenied = Date.now();
  111. }
  112. if (data.type === "competitor") {
  113. if (data.competitor !== uuid) {
  114. lastTimeCompetitor = Date.now();
  115. }
  116. }
  117. },
  118. attemptPing() {
  119. if (!gotPong) {
  120. if (pingTries < 10) {
  121. pingTries += 1;
  122. this.sendEvent("ping", null);
  123. setTimeout(() => {
  124. this.attemptPing.apply(this);
  125. }, 1000);
  126. } else {
  127. new Toast(
  128. "Couldn't connect to ActivityWatch Musare extension."
  129. );
  130. }
  131. }
  132. }
  133. };