forked from palashkaria/roam-modifiers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
time-format.user.js
65 lines (60 loc) · 2 KB
/
time-format.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// ==UserScript==
// @name Roam Current time format
// @namespace http://tampermonkey.net/
// @version 0.2
// @description use AM/PM format for /currentTime command in Roam Research
// @downloadURL https://raw.github.com/palashkaria/roam-modifiers/master/time-format.user.js
// @author palashkaria
// @match https://roamresearch.com/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0' + minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}
function replaceTime(mutations) {
var textarea = document.querySelector('textarea');
if (textarea) {
textarea.addEventListener('keydown', (data) => {
console.log('value', data.target.value);
var currentDate = new Date();
var currentTime = currentDate
.toLocaleTimeString()
.split(':')
.splice(0, 2)
.join(':');
var currentTimeFormatted = formatAMPM(currentDate);
console.log(
'value',
data.target.value,
currentTimeFormatted,
currentTime
);
if (currentTime === data.target.value) {
console.log('yas!');
document.querySelector('textarea').value = currentTimeFormatted;
}
});
}
}
var target = document.querySelector('body');
// create an observer instance
var observer = new MutationObserver(function (mutations) {
replaceTime(mutations);
mutations.forEach(function (mutation) {
console.log(mutation.type);
});
});
// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true };
// pass in the target node, as well as the observer options
observer.observe(target, config);
})();