Skip to content

Instantly share code, notes, and snippets.

@Patabugen
Last active December 7, 2022 01:54
Show Gist options
  • Save Patabugen/2c2c15166e9d66f8826bab69802ccd2e to your computer and use it in GitHub Desktop.
Save Patabugen/2c2c15166e9d66f8826bab69802ccd2e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name New Userscript
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match https://app.sunsama.com/*focus=true*
// @que
// @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant none
// @noframes
// ==/UserScript==
(function() {
'use strict';
if (document.location.href.indexOf('focus=true') < 0) {
return;
}
if (document.SunsamaClockWatcher !== undefined) {
return;
}
class SunsamaClockWatcher {
constructor() {
this.checkerInterval = undefined;
}
stopChecking()
{
console.log('Clearing ' + this.checkerInterval);
clearInterval(this.checkerInterval);
this.checkerInterval = undefined;
return this;
}
waitForTimer()
{
console.log('starting to wait');
return new Promise((resolve, reject) => {
console.log('insiude the promise');
this.checkerInterval = setInterval(function(resolve, watcher){
console.log('checking');
let btn = document.querySelector('div.task-timing-play-btn');
if (btn !== null) {
console.log(watcher.checkerInterval);
console.log('found it');
watcher.stopChecking();
resolve(btn);
}
}, 2500, resolve, watcher);
});
}
startObservers(btn) {
console.dir(btn);
let options = {
attributes: true,
attributeOldValue :true
}
function callback(mutationList, obsever) {
mutationList.forEach(function(mutation) {
if (mutation.type != 'attributes' && mutation.attributeName != 'class' ) {
return;
}
if (mutation.oldValue == 'task-timing-play-btn inactive') {
console.log('Timer Started');
// Todo: Trigger some callbacks so we can update Toggl
} else if (mutation.oldValue == 'task-timing-play-btn ') {
// Todo: Trigger some callbacks so we can update Toggl
console.log('Timer Stopped');
} else {
// console.dir(mutation);
}
});
}
const observer = new MutationObserver(callback);
observer.observe(btn, options);
}
}
const watcher = new SunsamaClockWatcher;
document.SunsamaClockWatcher = watcher;
console.log('starting');
watcher.waitForTimer().then(watcher.startObservers);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment