snippets/src/user-script/kaiten-insert-jira-link.tem...

111 lines
3.6 KiB
JavaScript

// ==UserScript==
// @name Profee Kaiten -> insert Jira link
// @namespace http://tampermonkey.net/
// @version 0.1
// @description helps to insert link to Profee Jira in Kaiten Card
// @author bvn13
// @match https://profeelab.kaiten.ru/space/*/card/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=kaiten.ru
// @grant none
// @require https://raw.githubusercontent.com/dwachss/bililiteRange/master/bililiteRange.js
// @require https://code.jquery.com/jquery-2.1.4.min.js
// @require https://raw.githubusercontent.com/jquery/jquery-simulate/main/jquery.simulate.js
// @require https://raw.githubusercontent.com/j-ulrich/jquery-simulate-ext/master/src/jquery.simulate.ext.js
// @require https://raw.githubusercontent.com/j-ulrich/jquery-simulate-ext/master/src/jquery.simulate.key-sequence.js
// ==/UserScript==
function exists(elem) {
return typeof(elem) != 'undefined' && elem != null;
}
// https://stackoverflow.com/a/14234618/2798461
function collectionHas(a, b) {
for(var i = 0, len = a.length; i < len; i ++) {
if(a[i] == b) return true;
}
return false;
}
;
function findParentBySelector(elm, selector) {
var all = document.querySelectorAll(selector);
var cur = elm.parentNode;
while(cur && !collectionHas(all, cur)) {
cur = cur.parentNode; //go up
}
return cur; //will return null if not found
}
;
// https://github.com/j-ulrich/jquery-simulate-ext/tree/7285c77ecf8c5ee7258f6317e601a281aaec04c2#usage
function setTextAreaValue(textarea, value) {
if (exists(textarea)) {
setTimeout(function() {
jQuery(textarea).focus();
//jQuery(textarea).html(value);
jQuery(textarea).simulate("key-sequence", {sequence: value});
//jQuery(textarea).val(value);
//jQuery(textarea).text(value);
}, 100);
} else {
console.log("textarea dos not exist");
}
}
;
function getJiraLabel(url) {
var task = url.substring(url.lastIndexOf('/') + 1)
if (url.startsWith("https://jira.local.profee.com/browse/")) {
return task;
} else {
console.log("'" + url + "' not started with 'https://jira.local.profee.com/browse/'");
}
return "";
}
;
function doc_keyUp(e) {
//if (e.ctrlKey && e.key === 'ArrowDown') {
if (!e.shiftKey && !e.ctrlKey && e.key === 'Tab') {
var activeElement = document.activeElement;
var parent = findParentBySelector(document.activeElement, "div[data-test='newExternalLink']");
if (exists(parent)) {
window.lastLink = activeElement; // save for shift+tab
var url = jQuery(activeElement).val();
console.log(url);
var button = jQuery(parent).find("button:first");
var textarea = undefined;
if (exists(button)) {
if (jQuery(button).text() == 'Добавить описание') {
jQuery(button).click();
textarea = document.activeElement;
} else {
textarea = jQuery(parent).find("textarea:first");
}
}
setTextAreaValue(textarea, getJiraLabel(url));
} else {
console.log("parent not found");
}
}
if (e.shiftKey && e.key === 'Tab') {
if (exists(window.lastLink)) {
setTimeout(function() {
jQuery(window.lastLink).focus();
}, 100);
}
}
}
;
(function() {
'use strict';
// register the handler
document.addEventListener('keyup', doc_keyUp, false);
})();