PR Helper for Workplace

Pre-filled PR info in Workplace

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

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.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==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();
  }
})();