open_original_links_in_pocket

just open direct!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        open_original_links_in_pocket
// @namespace   undegro
// @include     https://getpocket.com/*
// @version     4.6
// @license     MIT
// @description just open direct!
// ==/UserScript==

{
  const parent = document.getElementById("__next")
  const observeParent = new MutationObserver(records => records.filter(record => record.addedNodes[0]).some(record => record.target.tagName == "MAIN" || (record.target == parent && record.addedNodes[0].querySelector("main"))) && init())
  const observeArticle = new MutationObserver(records => records.flatMap(record => [...record.addedNodes]).forEach(rewrite))

  observeParent.observe(parent, { childList: true, subtree: true })

  async function init() {
    observeArticle.disconnect()
    const articleList = await waitElement("main > div > div > div")
    if(articleList) observeArticle.observe(articleList, { childList: true, subtree: true })
  }

  function rewrite(target) {
    if(target.tagName == "ARTICLE") {
      const links = [...target.getElementsByTagName("a")]
      const publisherLink = links.find(link => link.classList.contains("publisher"))
      links.forEach(link => {
        link.addEventListener("click", e => e.stopPropagation())
        link.href = decodeURIComponent(publisherLink.href.replace(/^https:\/\/getpocket\.com\/redirect\?url=/, ""))
        link.setAttribute("target", "_blank")
      })
    }
  }

  function waitElement(selector) {
    return new Promise((resolve) => {
      const searchElement = () => {
        const elem = document.querySelector(selector)
        if(!elem) {
          requestAnimationFrame(searchElement)
        }else{
          resolve(elem)
        }
      }

      searchElement()
      setTimeout(() => resolve(false), 5000)
    })
  }
}