Last active
September 23, 2020 11:51
-
-
Save gijsepping/d38e18a353cc9493d5eb7e069b620baa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This little script fetches a json array from outlook with integromat and php | |
Json looks like this: | |
["08:00 - 17:00 All day event","09:00 - 09:15 agenda row 2","10:00 - 17:00 agenda row 3"] | |
Just type :agenda: and it will fetches the array and write the newlines. | |
*/ | |
document.addEventListener('input', function(e){ | |
if ('_webhookHook' in window) { | |
setTimeout(function(){ window._webhookHook(e); }, 0); | |
} | |
}); | |
window._webhookHook = async function(e) { | |
// logging | |
window._e = e; | |
// exit if not target | |
var elem = e.target | |
var res = elem.value.match(/:agenda:/g); | |
if(res){ | |
elem.value = elem.value.replace(':agenda:', ' '); | |
var nativeSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype,"value").set; | |
nativeSetter.call(elem, "**AGENDA**"); | |
(async () => { | |
const rawResponse = await fetch('url to script that fetches json array'); | |
const data = await rawResponse.json(); | |
await KeyboardLib.pressEnter(); | |
await KeyboardLib.pressTab(); | |
var i; | |
for (i = 0; i < data.length; i++) { | |
elem.focus(); | |
elem = KeyboardLib.getActiveEditElement(); | |
line = data[i]; | |
nativeSetter.call(elem, line); | |
elem.dispatchEvent(new Event('input', {bubbles: true, cancelable: true })); | |
await KeyboardLib.pressEnter(); | |
await KeyboardLib.delay(150); | |
} | |
await KeyboardLib.pressShiftTab(); | |
})(); | |
} | |
} | |
window.KeyboardLib = { | |
// thank you @VladyslavSitalo for the awesome Roam Toolkit, and the basis for this code | |
LEFT_ARROW: 37, | |
UP_ARROW: 38, | |
RIGHT_ARROW: 39, | |
DOWN_ARROW: 40, | |
BASE_DELAY: 20, | |
delay(millis) { | |
return new Promise(resolve => setTimeout(resolve, millis)) | |
}, | |
getKeyboardEvent: function(type, code, opts) { | |
return new KeyboardEvent(type, { | |
bubbles: true, | |
cancelable: true, | |
keyCode: code, | |
...opts, | |
}) | |
}, | |
getActiveEditElement: function() { | |
// stolen from Surfingkeys. Needs work. | |
var element = document.activeElement | |
// on some pages like chrome://history/, input is in shadowRoot of several other recursive shadowRoots. | |
while (element.shadowRoot) { | |
if (element.shadowRoot.activeElement) { | |
element = element.shadowRoot.activeElement | |
} else { | |
var subElement = element.shadowRoot.querySelector('input, textarea, select') | |
if (subElement) { | |
element = subElement | |
} | |
break | |
} | |
} | |
return element | |
}, | |
async simulateSequence(events, delayOverride) { | |
;events.forEach(function(e){ | |
return KeyboardLib.getActiveEditElement().dispatchEvent(KeyboardLib.getKeyboardEvent(e.name, e.code, e.opt)); | |
}); | |
return this.delay(delayOverride || this.BASE_DELAY); | |
}, | |
async simulateKey(code, delayOverride, opts) { | |
return this.simulateSequence([{name:'keydown', code:code, opt:opts}, {name:'keyup', code:code, opt:opts}], delayOverride); | |
}, | |
async changeHeading(heading, delayOverride) { | |
return this.simulateSequence( | |
[ | |
{name:'keydown', code:18, opt:{altKey:true}}, | |
{name:'keydown', code:91, opt:{metaKey:true}}, | |
{name:'keydown', code:48+heading, opt:{altKey:true, metaKey:true}}, | |
{name:'keyup', code:91, opt:{altKey:true}}, | |
{name:'keyup', code:18, opt:{}} | |
], | |
delayOverride); | |
}, | |
async pressEnter(delayOverride) { | |
return this.simulateKey(13, delayOverride) | |
}, | |
async pressEsc(delayOverride) { | |
return this.simulateKey(27, delayOverride) | |
}, | |
async pressBackspace(delayOverride) { | |
return this.simulateKey(8, delayOverride) | |
}, | |
async pressTab(delayOverride) { | |
return this.simulateKey(9, delayOverride) | |
}, | |
async pressShiftTab(delayOverride) { | |
return this.simulateKey(9, delayOverride, {shiftKey: true}) | |
}, | |
async pressCtrlV(delayOverride) { | |
return this.simulateKey(118, delayOverride, {metaKey: true}) | |
}, | |
getInputEvent() { | |
return new Event('input', { | |
bubbles: true, | |
cancelable: true, | |
}) | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment