Imaglr Swap Actions

Swap left and right action groups in streaming layout posts

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Imaglr Swap Actions
// @namespace    imaglr.com/
// @version      1.0.0
// @description  Swap left and right action groups in streaming layout posts
// @match        https://*imaglr.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function () {
  'use strict';

  const FOOTER_SELECTOR = 'footer.stream-post-actions';
  const LEFT_SELECTOR = '.stream-post-actions-left';
  const RIGHT_SELECTOR = '.stream-post-actions-right';

  function swapActions(footer) {
    if (!footer || footer.dataset.tmActionsSwapped === 'true') {
      return;
    }

    const left = footer.querySelector(LEFT_SELECTOR);
    const right = footer.querySelector(RIGHT_SELECTOR);

    if (!left || !right) {
      return;
    }

    /*
      Move the right group before the left group.

      Original:
        footer
          .stream-post-actions-left
          .stream-post-actions-right

      Result:
        footer
          .stream-post-actions-right
          .stream-post-actions-left
    */
    footer.insertBefore(right, left);

    footer.dataset.tmActionsSwapped = 'true';
  }

  function swapAllActions(root = document) {
    root.querySelectorAll(FOOTER_SELECTOR).forEach(swapActions);
  }

  // Initial run
  swapAllActions();

  // Handle dynamically added posts
  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      for (const node of mutation.addedNodes) {
        if (!(node instanceof Element)) {
          continue;
        }

        if (node.matches?.(FOOTER_SELECTOR)) {
          swapActions(node);
        } else {
          swapAllActions(node);
        }
      }
    }
  });

  observer.observe(document.body, {
    childList: true,
    subtree: true
  });
})();