// ==UserScript==
// @name WooCommerce Live Branches
// @namespace https://wordpress.com/
// @version 1.0
// @description Adds links to PRs pointing to Jurassic Ninja sites for live-testing a changeset
// @grant GM_xmlhttpRequest
// @connect jurassic.ninja
// @require https://code.jquery.com/jquery-3.3.1.min.js
// @match https://github.com/woocommerce/woocommerce/pull/*
// ==/UserScript==
// Need to declare "jQuery" for linting within TamperMonkey, but in the monorepo it's already declared.
// eslint-disable-next-line no-redeclare
/* global jQuery */
( function () {
const $ = jQuery.noConflict();
const markdownBodySelector = '.pull-discussion-timeline .markdown-body';
let pluginsList = null;
// Watch for relevant DOM changes that indicate we need to re-run `doit()`:
// - Adding a new `.markdown-body`.
// - Removing `#woocommerce-live-branches`.
const observer = new MutationObserver( ( list ) => {
for ( const m of list ) {
for ( const n of m.addedNodes ) {
if (
( n.matches && n.matches( markdownBodySelector ) ) ||
( n.querySelector &&
n.querySelector( markdownBodySelector ) )
) {
doit();
return;
}
}
for ( const n of m.removedNodes ) {
if (
n.id === 'woocommerce-live-branches' ||
( n.querySelector &&
n.querySelector( '#woocommerce-live-branches' ) )
) {
doit();
return;
}
}
}
} );
observer.observe( document, { subtree: true, childList: true } );
// Run it on load too.
doit();
/**
* Determine the current repo.
*
* Currently looks at the URL, expecting it to match a `@match` pattern from the script header.
*
* @returns {string|null} Repo name.
*/
function determineRepo() {
const m = location.pathname.match( /^\/([^/]+\/[^/]+)\/pull\// );
return m && m[ 1 ] ? decodeURIComponent( m[ 1 ] ) : null;
}
/** Function. */
function doit() {
const markdownBody =
document.querySelectorAll( markdownBodySelector )[ 0 ];
if (
! markdownBody ||
markdownBody.querySelector( '#woocommerce-live-branches' )
) {
// No body or Live Branches is already there, no need to do it again.
return;
}
const host = 'https://jurassic.ninja';
const currentBranch = jQuery( '.head-ref:first' ).text();
const branchIsForked = currentBranch.includes( ':' );
const branchStatus = $( '.gh-header-meta .State' ).text().trim();
const repo = determineRepo();
if ( branchStatus === 'Merged' ) {
const contents = `
`;
}
/**
* Append HTML to the element.
*
* Also registers `onInputChanged()` as a change handler for all checkboxes in the HTML.
*
* @param {HTMLElement} el - Element.
* @param {string} contents - HTML to append.
*/
function appendHtml( el, contents ) {
const $el = $( el );
const liveBranches = $(
''
).append( `