aw.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. function abc(c) {
  59. // eslint-disable-next-line
  60. const r = (Math.random() * 16) | 0;
  61. // eslint-disable-next-line
  62. const v = c == "x" ? r : (r & 0x3) | 0x8;
  63. return v.toString(16);
  64. }
  65. );
  66. document.addEventListener(
  67. "ActivityWatchMusareEvent",
  68. this.eventListener
  69. );
  70. notConnectedToast.show();
  71. this.attemptPing();
  72. enabled = true;
  73. console.log("Enabled AW.");
  74. setInterval(() => {
  75. if (lastTimeDenied + 1000 > Date.now()) {
  76. deniedToast.show();
  77. } else {
  78. deniedToast.hide();
  79. }
  80. if (lastTimeCompetitor + 1000 > Date.now()) {
  81. competitorToast.show();
  82. } else {
  83. competitorToast.hide();
  84. }
  85. if (
  86. !(lastTimeDenied + 1000 > Date.now()) &&
  87. !(lastTimeCompetitor + 1000 > Date.now()) &&
  88. lastTimeSentVideoDate + 1000 > Date.now()
  89. ) {
  90. sendingVideoDataToast.show();
  91. } else {
  92. sendingVideoDataToast.hide();
  93. }
  94. }, 1000);
  95. }
  96. },
  97. disable() {
  98. document.removeEventListener(
  99. "ActivityWatchMusareEvent",
  100. this.eventListener
  101. );
  102. enabled = false;
  103. notConnectedToast.hide();
  104. console.log("Disabled AW.");
  105. },
  106. eventListener(event) {
  107. const data = event.detail;
  108. if (data.type === "pong") {
  109. gotPong = true;
  110. notConnectedToast.hide();
  111. new Toast("Got pong, connected to ActivityWatch Musare extension");
  112. }
  113. if (data.type === "denied") {
  114. lastTimeDenied = Date.now();
  115. }
  116. if (data.type === "competitor") {
  117. if (data.competitor !== uuid) {
  118. lastTimeCompetitor = Date.now();
  119. }
  120. }
  121. },
  122. attemptPing() {
  123. if (!gotPong) {
  124. if (pingTries < 10) {
  125. pingTries += 1;
  126. this.sendEvent("ping", null);
  127. setTimeout(() => {
  128. this.attemptPing.apply(this);
  129. }, 1000);
  130. } else {
  131. new Toast(
  132. "Couldn't connect to ActivityWatch Musare extension."
  133. );
  134. }
  135. }
  136. }
  137. };