NOTICE: By continued use of this site you understand and agree to the binding Terms of Service and Privacy Policy.
// ==UserScript== // @name Automate time grep from Redmine // @description click outside google docs editor window then press Alt+q to activate script // @require https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js // @require https://bundle.run/hotkeys-js@3.7.3 // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @require https://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js // @updateURL https://openuserjs.org/meta/anafema/Automate_time_grep_from_Redmine.meta.js // @downloadURL https://openuserjs.org/install/anafema/Automate_time_grep_from_Redmine.user.js // @match *docs.google.com/* // @grant GM_xmlhttpRequest // @licence MIT // @version 0.4 // ==/UserScript== var from = '11/21/2020'; var to = '11/27/2020'; var foo; hotkeys('alt+q', function (event, handler) { //foo = prompt("", from + "---" + to); foo = prompt("", 'From '+from+' to '+to); console.log(foo); from = Date.parse(from).toString('dd/MM/yyyy'); to = Date.parse(to).toString('dd/MM/yyyy'); from = foo.match(/(\d*\/\d*\/\d*)/g)[0]; to = foo.match(/(\d*\/\d*\/\d*)/g)[1]; from = Date.parse(from).toString('yyyy-MM-dd'); to = Date.parse(to).toString('yyyy-MM-dd'); console.log(from); console.log(to); var link = "https://redmine.techstudio.tv/time_entries?utf8=✓&f[]=spent_on&op[spent_on]=><&v[spent_on][]=" + from + "&v[spent_on][]=" + to + "&f[]=user_id&op[user_id]=%3D&v[user_id][]=me&f[]=&c[]=project&c[]=spent_on&c[]=user&c[]=activity&c[]=issue&c[]=comments&c[]=hours"; getUrlWCockies(link); }); var date_diff_indays = function(date1, date2) { var dt1 = new Date(date1); var dt2 = new Date(date2); return Math.floor((Date.UTC(dt2.getFullYear(), dt2.getMonth(), dt2.getDate()) - Date.UTC(dt1.getFullYear(), dt1.getMonth(), dt1.getDate()) ) /(1000 * 60 * 60 * 24)); } function getUrlWCockies(string) { var global_return; //--- Global variables var mashupURL = string; var cookGenURL = string; if (location.href == cookGenURL) { //--- May be best we can do until Greasemonkey fixes tab handling flaws. document.title = "Close me!"; if (window.opener) { waitForKeyElements("i:contains(openedByGM)", closePopupIfCan); } } else { attemptMashup(); window.addEventListener("message", receiveCookieMessage, false); } //-- Just functions from here on down... function closePopupIfCan(jNode) { window.opener.postMessage("Cookie(s) should be set!", "*"); window.close(); } function attemptMashup() { GM_xmlhttpRequest({ method: "GET", url: mashupURL, onload: parseDictionaryResponse, onabort: reportAJAX_Error, onerror: reportAJAX_Error, ontimeout: reportAJAX_Error }); } function receiveCookieMessage(event) { if (event.origin != "http://lema.rae.es") return; console.log("message ==> ", event.data); /*--- Now that have cookie(s), re-attempt mashup, but need a little settling time. */ setTimeout(attemptMashup, 888); } async function parseDictionaryResponse(respObject) { if (respObject.status != 200 && respObject.status != 304) { reportAJAX_Error(respObject); return; } /*--- If the required cookie is not present/valid, open the target page in a temporary tab to set the cookies, then reload this page. The test string is unique to the scraped site and is only present when cookie(s) is/are needed. */ if (/function\s+challenge/i.test(respObject.responseText)) { var newTab = window.open(cookGenURL); return; } //--- Don't use jQuery to parse this! var parser = new DOMParser(); var responseDoc = parser.parseFromString( respObject.responseText, "text/html" // Firefox only, for now. ); //--- Get site-specific payload and put in site-specific location. var payload = responseDoc.querySelectorAll("div.total-hours"); //console.log(payload.toString()); //console.log( payload[0]); Main(payload[0]); //console.log(responseDoc.find("div")); //var jparsed = jQuery.parseHTML(responseDoc.toString()); //console.log(jparsed.find("div")); } function reportAJAX_Error(respObject) { alert( 'Error ' + respObject.status + '! "' + respObject.statusText + '"' ); } } function Main(string) { resp_text = $(string).html(); console.log(resp_text); if (resp_text == undefined) { alert("Please log in to Redmine"); } var matches_array = resp_text.match(/">(\d*)<.*">(.\d*)/); var time = matches_array[1].toString().concat(((60 * parseFloat(matches_array[2])) / 100).toString().slice(1)); console.log(time); prompt(foo + " Days counted: " + (date_diff_indays(from,to) + 1).toString(), time); }