PR Helper for Workplace

Pre-filled PR info in Workplace

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         PR Helper for Workplace
// @namespace    https://github.com/Hephaest/helper-scripts
// @version      1.0
// @description  Pre-filled PR info in Workplace
// @author       Hephaest
// @icon         https://avatars.githubusercontent.com/u/37981444?v=4
// @match        https://www.workplace.com/
// @grant        none
// @license      MIT
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  var whiteList = [];
  var messageDivElement = null;
  var dataTransfer = new DataTransfer();

  var timer = setInterval(function () {
    messageDivElement = document.querySelector('div[contenteditable]');
    if (!messageDivElement || messageDivElement.onkeyup) return;
    messageDivElement.onkeyup = setUpMessageListener;
  }, 3000);

  function setUpMessageListener(event) {
    var input = event.target.innerText;
    if (!isInWhiteList() || !hasPRCmdTriggered(input)) return;
    setUpPRTemplate(event);
    //clearInterval(timer);
  }

  function isInWhiteList() {
    var pathname = window.location.pathname;
    var regex = /\/chat\/t\//gm;
    var chatId = pathname.replace(regex, '');
    var result = whiteList.includes(chatId);
    return result;
  }

  function hasPRCmdTriggered(input) {
    return input === 'PRT' || input === 'prt';
  }

  function setUpPRTemplate(event) {
    var templateText =
      ':\n' +
      'PR:\n' +
      'ChangeLog:\n' +
      'Reviewers:\n' +
      '辛苦各位有空的时候帮忙看一下🙏';
    dispatchPaste(event.target, templateText);
  }

  function dispatchPaste(target, text) {
    dataTransfer.setData('text/plain', text);

    target.dispatchEvent(
      new ClipboardEvent('paste', {
        clipboardData: dataTransfer,
        // need these for the event to reach Draft paste handler
        bubbles: true,
        cancelable: true,
      })
    );

    // clear DataTransfer Data
    dataTransfer.clearData();
  }
})();