aw.ts 3.3 KB

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