Skip to content

Instantly share code, notes, and snippets.

@and-rom
Last active May 21, 2020 13:01
Show Gist options
  • Save and-rom/926a0e79fb1ecfc6f673c68b24442560 to your computer and use it in GitHub Desktop.
Save and-rom/926a0e79fb1ecfc6f673c68b24442560 to your computer and use it in GitHub Desktop.
OWA Hot Keys
// ==UserScript==
// @name OWA Hot Keys
// @namespace https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560
// @version 0.0.2
// @author and-rom
// @description try to take over the world!
// @homepage https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560
// @updateURL https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560/raw/owahotkeys.meta.js
// @downloadURL https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560/raw/owahotkeys.user.js
// @match https://mail.voskhod.ru/owa/*
// @grant none
// @run-at document-idle
// ==/UserScript==
// ==UserScript==
// @name OWA Hot Keys
// @namespace https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560
// @version 0.0.2
// @author and-rom
// @description try to take over the world!
// @homepage https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560
// @updateURL https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560/raw/owahotkeys.meta.js
// @downloadURL https://gist.github.com/and-rom/926a0e79fb1ecfc6f673c68b24442560/raw/owahotkeys.user.js
// @match https://mail.voskhod.ru/owa/*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function() {
'use strict';
function insertText(text, mode) {
if (mode) {
console.log("html mode");
let target = document.querySelector('iframe#EditorBody').contentWindow.document;
//target.getElementById('ms-rterangecursor-start').remove();
//target.getElementById('ms-rterangecursor-end').remove();
//target.querySelector('div#MicrosoftOWAEditorRegion p').innerHTML = text.indexOf('[[CURSOR]]') > -1 ? text.replace('[[CURSOR]] ','<span id="ms-rterangecursor-start" rtenodeid="2"></span><span id="ms-rterangecursor-end"></span>') : text + '<span id="ms-rterangecursor-start" rtenodeid="2"></span><span id="ms-rterangecursor-end"></span>';
target.querySelector('div#MicrosoftOWAEditorRegion p').innerHTML = text.replace('[[CURSOR]]','CURSOR');
} else {
console.log("text mode");
let pos=text.indexOf('[[CURSOR]]') > -1 ? text.indexOf('[[CURSOR]]') : text.length;
let txtarea = document.querySelector('textarea[aria-label="Тело сообщения"]');
txtarea.value=text.replace('[[CURSOR]]','') + "\n" + txtarea.value.split('\n').slice(1).join('\n');
txtarea.focus();
txtarea.selectionEnd = pos;
txtarea.selectionStart = pos;
}
}
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutationList, observer) {
mutationList.forEach(function( mutation ) {
var newNodes = mutation.addedNodes;
if (newNodes !== null && newNodes.length !== 0) {
//console.log(newNodes);
newNodes.forEach(function( newNode ) {
if (newNode.tagName === "DIV" && newNode.id == "EditorFormatBar") {
console.log( "Editor opened" );
editor=true;
setEL();
}
});
}
var removedNodes = mutation.removedNodes;
if (removedNodes !== null && removedNodes.length !== 0) {
removedNodes.forEach(function( removedNode ) {
if (removedNode.tagName === "DIV" && removedNode.className == "conductorContent" && editor) {
console.log( "Editor closed" );
editor=false;
targetDoc.removeEventListener('keydown', quickText, false);
}
});
}
});
});
observer.observe(document, {
childList: true,
subtree: true
});
var htmlMode=false;
var targetDoc;
var editor=false;
var quickText = function (e) {
var code = (e.keyCode ? e.keyCode : e.which);
console.log("keydown:" + code);
if (e.altKey && [49,50,51,52].indexOf(code) > -1) {
e.stopPropagation();
e.preventDefault();
switch (code) {
case 49:
insertText("Готово.",htmlMode);
break;
case 50:
insertText("Объект добавлен и активирован.",htmlMode);
break;
case 51:
insertText("Объекты добавлены и активированы.",htmlMode);
break;
case 52:
insertText("Объекта с кодом [[CURSOR]] в РПЦ ИРКД не зарегистрировано.",htmlMode);
break;
default:
console.log( "Нет таких значений" );
}
}
}
function setEL() {
var text = document.querySelector('textarea[aria-label="Тело сообщения"]').value
console.log(text);
if (text == "" ) {
console.log("html mode detect");
htmlMode=true;
targetDoc = document.querySelector('iframe#EditorBody').contentWindow;
}
else {
console.log("text mode detect");
htmlMode=false;
targetDoc = window;
}
console.log( "addEventListener on" );
console.log( targetDoc );
targetDoc.addEventListener('keydown', quickText, false);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment