123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- import Toast from "toasters";
- import { generateUuid } from "@common/utils/generateUuid";
- let gotPong = false;
- let pingTries = 0;
- let uuid = null;
- let enabled = false;
- let lastTimeSentMediaDate = 0;
- let lastTimeDenied = 0;
- let lastTimeCompetitor = 0;
- const notConnectedToast = new Toast({
- content: "ActivityWatch is not connected yet.",
- persistent: true,
- interactable: false
- });
- notConnectedToast.hide();
- const sendingMediaDataToast = new Toast({
- content: "Sending media data to ActivityWatch.",
- persistent: true,
- interactable: false
- });
- sendingMediaDataToast.hide();
- const deniedToast = new Toast({
- content:
- "Another Musare instance is already sending data to ActivityWatch Musare extension. Please only use 1 active tab for stations and editsong.",
- persistent: true,
- interactable: false
- });
- deniedToast.hide();
- const competitorToast = new Toast({
- content:
- "Another Musare instance is already sending data to ActivityWatch Musare extension. Please only use 1 active tab for stations and editsong.",
- persistent: true,
- interactable: false
- });
- competitorToast.hide();
- export default {
- sendMediaData(mediaData) {
- if (enabled) {
- if (!gotPong) return false;
- if (lastTimeDenied + 1000 > Date.now()) return false;
- if (lastTimeCompetitor + 1000 > Date.now()) return false;
- lastTimeSentMediaDate = Date.now();
- this.sendEvent("videoData", mediaData);
- }
- return true;
- },
- sendEvent(type, data) {
- if (enabled)
- document.dispatchEvent(
- new CustomEvent("ActivityWatchMusareEvent", {
- detail: {
- type,
- source: uuid,
- data
- }
- })
- );
- },
- enable() {
- if (!enabled) {
- uuid = generateUuid();
- document.addEventListener(
- "ActivityWatchMusareEvent",
- this.eventListener
- );
- notConnectedToast.show();
- this.attemptPing();
- enabled = true;
- console.log("Enabled AW.");
- setInterval(() => {
- if (lastTimeDenied + 1000 > Date.now()) {
- deniedToast.show();
- } else {
- deniedToast.hide();
- }
- if (lastTimeCompetitor + 1000 > Date.now()) {
- competitorToast.show();
- } else {
- competitorToast.hide();
- }
- if (
- !(lastTimeDenied + 1000 > Date.now()) &&
- !(lastTimeCompetitor + 1000 > Date.now()) &&
- lastTimeSentMediaDate + 1000 > Date.now()
- ) {
- sendingMediaDataToast.show();
- } else {
- sendingMediaDataToast.hide();
- }
- }, 1000);
- }
- },
- disable() {
- document.removeEventListener(
- "ActivityWatchMusareEvent",
- this.eventListener
- );
- enabled = false;
- notConnectedToast.hide();
- console.log("Disabled AW.");
- },
- eventListener(event) {
- const data = event.detail;
- if (data.type === "pong") {
- gotPong = true;
- notConnectedToast.hide();
- new Toast("Got pong, connected to ActivityWatch Musare extension");
- }
- if (data.type === "denied") {
- lastTimeDenied = Date.now();
- }
- if (data.type === "competitor") {
- if (data.competitor !== uuid) {
- lastTimeCompetitor = Date.now();
- }
- }
- },
- attemptPing() {
- if (!gotPong) {
- if (pingTries < 10) {
- pingTries += 1;
- this.sendEvent("ping", null);
- setTimeout(() => {
- this.attemptPing.apply(this);
- }, 1000);
- } else {
- new Toast(
- "Couldn't connect to ActivityWatch Musare extension."
- );
- }
- }
- }
- };
|