User:Alexis Jazz/OMGTB.js

// ==UserScript== // @name    omgtb Phabricator // @version 1 // @grant   none // @match   https://phabricator.wikimedia.org/T* // @run-at	document-end // ==/UserScript==

/* https://phabricator.wikimedia.org/T344999#9121885 Greasemonkey script that adds a "Mark as train blocker" link on Phabricator. Marking tasks as train blockers is cumbersome otherwise.

This code is hereby irrevocably released as WTFPL Version 2[www.wtfpl.net/about/]

This script checks if the viewed task may be a potential train blocker you just filed and opens the "add parent task" screen. There is a line of code you can uncomment to automatically submit as well, but unless https://phabricator.wikimedia.org/T344999 gets approved this is probably not a good idea as beta-cluster-reproducible is too broad

It also adds a "Mark as train blocker" link

omgtb = Oh My God Train Blocker

To test this script raise the default value of 120000 for maximum age to, I dunno, 157680000000 (5 years) and find a task YOU filed with the Beta-Cluster-reproducible tag. Just paste the script in the console

much of this BS is because the task creation form doesn't allow the parent task to be specified, so we have to check whenever viewing a task: 1. if you filed it 2. if you filed it RECENTLY total bloody waste if you ask me and no elements have reasonable classes or IDs so I made this brilliant function called omgtb.getElThatReads which does something absolutely hilarious hint: it only works in English /*jshint esversion:8*/ async function getTaskList { omgtb.taskListHTTP = await fetch('https://train-blockers.toolforge.org/api.php'); omgtb.taskList = await omgtb.taskListHTTP.text; omgtb.setParentTask(omgtb.taskList); } if ( document.location.href.match(/https:\/\/phabricator\.wikimedia\.org\/T[0-9]+$/) ) { //only run on task pages var omgtb = {};

omgtb.strText = {};

omgtb.strText.en = { 'Authored By':'Authored By', 'Flag For Later':'Flag For Later', 'Select':'Select', 'Save Parent Tasks':'Save Parent Tasks', 'Edit Related Tasks...':'Edit Related Tasks...', 'Edit Parent Tasks':'Edit Parent Tasks', 'tagname':'Beta-Cluster-reproducible' };	omgtb.lang = 'en'; omgtb.str = omgtb.strText[omgtb.lang]; omgtb.WhoAmI = document.getElementsByClassName('phabricator-core-user-menu')[1].href.replace(/.*\/([^\/]+)\/$/,'$1'); omgtb.getElThatReads = function(selector,linkInnerText,int2) { //returns element that matches query and innerText omgtb.linkEls = document.body.querySelectorAll(selector); for (int2=0;int2<omgtb.linkEls.length;int2++){ if ( omgtb.linkEls[int2] && omgtb.linkEls[int2].innerText.match(linkInnerText) ) { return omgtb.linkEls[int2]; }		}	};	omgtb.TaskFiler = omgtb.getElThatReads('.phui-curtain-panel-header',omgtb.str['Authored By']).parentElement.querySelectorAll('.phui-link-person')[0].href.replace(/.*\/([^\/]+)\/$/,'$1'); omgtb.tblink = omgtb.getElThatReads('.phabricator-action-view-item',omgtb.str['Flag For Later']).parentElement.cloneNode(true); omgtb.tblink.querySelectorAll('a')[0].innerHTML = ' Mark as train blocker '; omgtb.tblink.querySelectorAll('a')[0].href = ''; omgtb.tblink.id = 'trainblocklink'; omgtb.tblink.onclick = function{omgtb.force=1;getTaskList;}; omgtb.getElThatReads('.phabricator-action-view-item',omgtb.str['Flag For Later']).parentElement.parentElement.appendChild(omgtb.tblink); omgtb.waitForSearch = function { omgtb.int = 0; var waitForSearch = setInterval(function{ //yes I am evil			console.log('waiting for the search.. are we there yet mate?');			omgtb.int++;			if ( omgtb.int > 50 ) {				clearInterval(waitForSearch);			}     omgtb.searchResultCount = document.body.querySelectorAll('.phabricator-object-selector-results .phabricator-object-selector-row').length;			if ( omgtb.searchResultCount > 0 && omgtb.searchResultCount < 5 ) { //as we entered the exact task we want, the presence of a single row would mean it's done.. except sometimes this still returns more than one result				omgtb.getElThatReads('a.button',omgtb.str.Select).click;							clearInterval(waitForSearch);			}			omgtb.getElThatReads('button',omgtb.str['Save Parent Tasks']).focus;			//omgtb.getElThatReads('button',omgtb.str['Save Parent Tasks']).click; //submit parent task			},100); };	omgtb.guiHack = function { omgtb.getElThatReads('.phabricator-action-view-item',omgtb.str['Edit Related Tasks...']).click; omgtb.getElThatReads('.phabricator-action-view-item',omgtb.str['Edit Parent Tasks']).click; omgtb.int = 0; var InsertTask = setInterval(function{ //yes I am evil			console.log('waiting for parent task editing screen to open.. are we there yet mate?');			omgtb.int++;			if ( omgtb.int > 50 ) {				clearInterval(InsertTask);			}			if ( document.getElementsByClassName('phabricator-object-selector-search')[0] && document.getElementsByClassName('phabricator-object-selector-search')[0].querySelectorAll('input')[0] ) {				clearInterval(InsertTask);				document.getElementsByClassName('phabricator-object-selector-search')[0].querySelectorAll('input')[0].value = omgtb.trainBlockerTask;				document.getElementsByClassName('phabricator-object-selector-search')[0].querySelectorAll('input')[0].dispatchEvent(new KeyboardEvent('keydown', {					'keyCode': 37				})); //trigger search				omgtb.waitForSearch;			}		},100); };	omgtb.setParentTask = function(tbjson) { omgtb.trainBlockerTask = JSON.parse(tbjson).current.task_id; console.log('Train blocker task: '+omgtb.trainBlockerTask); if ( omgtb.getElThatReads('.phui-tag-core',omgtb.str.tagname) || omgtb.force ) { omgtb.guiHack; } else { console.log('couldn\'t find the tag mate'); }	};	if ( omgtb.WhoAmI == omgtb.TaskFiler ) { console.log('you\'re responsible for this task mate'); omgtb.FileDate = Date.parse(document.getElementsByClassName('phui-side-column')[0].querySelectorAll('.print-only')[0].innerText.replace(/ \(UTC.*$/,''));		omgtb.TimeSinceFiling = (new Date.getTime - omgtb.FileDate);		if ( omgtb.TimeSinceFiling > 0 && omgtb.TimeSinceFiling < 120000 ) { //120000ms = filed in the past 2 minutes			getTaskList;		} else {			console.log('nah whatever mate this is old news ('+omgtb.TimeSinceFiling+'ms old)');		}	} else {		console.log('you didn\'t file this task mate');	} } else {	console.log('this ain\'t no task mate'); } //