undegro / open_original_links_in_pocket

// ==UserScript==
// @name        open_original_links_in_pocket
// @namespace   undegro
// @include     https://app.getpocket.com/*
// @version     3.2
// @run-at      document-idle
// @updateURL   https://openuserjs.org/meta/undegro/open_original_links_in_pocket.meta.js
// @downloadURL https://openuserjs.org/install/undegro/open_original_links_in_pocket.user.js
// @license     MIT
// ==/UserScript==

{
  const root = document.getElementById("root"),
        option = {childList: true, subtree: true},
        wait = new MutationObserver(records => records.forEach(r => r.addedNodes[0] && r.addedNodes[0].className.includes("rgmu9k") && start() ))

  wait.observe(root, option);


  function start() {
    wait.disconnect()
    const pocket = document.getElementsByClassName("css-17egg64")[0],
          options = {childList: true, subtree: true},
          mo = new MutationObserver(records => records.forEach(r => r.addedNodes[0] && change(r.addedNodes))),
          change = list => Array.from(list).filter(e => e.tagName.includes("ARTICLE") && e.className.includes("5kqe7c"))
                             .forEach(e => {
                               let link = !e.getElementsByTagName("a")[2] ? e.getElementsByTagName("a")[0].href : e.getElementsByTagName("a")[2].href
                               e.getElementsByTagName("a")[0].href = e.getElementsByTagName("a")[1].href = decodeURIComponent(/\?url=(.*)/.exec(link)[1])
                             })

    mo.observe(pocket, options);

    change(pocket.getElementsByClassName("css-5kqe7c"));
  }
}