Pocket direct links

Clicking on an item directly opens the website, not the Pocket reader.

  1. // ==UserScript==
  2. // @name Pocket direct links
  3. // @version 5.0.1
  4. // @namespace http://www.agj.cl/
  5. // @description Clicking on an item directly opens the website, not the Pocket reader.
  6. // @license Unlicense
  7. // @match *://getpocket.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. const onLoad = (cb) =>
  12. /interactive|complete/.test(document.readyState)
  13. ? setTimeout(cb, 0)
  14. : document.addEventListener("DOMContentLoaded", cb);
  15. const sel = document.querySelector.bind(document);
  16. const selAll = document.querySelectorAll.bind(document);
  17. const onChanged = (el, cb) => {
  18. if (!el) throw `onChanged: No element passed.`;
  19. if (!cb) throw `onChanged: No callback passed.`;
  20. const observer = new MutationObserver(cb);
  21. observer.observe(el, { childList: true, subtree: true });
  22. return observer.disconnect.bind(observer);
  23. };
  24.  
  25. const attrFixedFlag = "data-link-fixed-agj";
  26. const getUrl = (el) => el.querySelector("a.publisher").getAttribute("href");
  27.  
  28. onLoad(() => {
  29. // Actual link fixing.
  30.  
  31. const fix = () => {
  32. Array.from(document.querySelectorAll("article")).forEach(fixOne);
  33. };
  34.  
  35. const fixOne = (el) => {
  36. const url = getUrl(el);
  37. if (!el.getAttribute(attrFixedFlag)) {
  38. const links = el.querySelectorAll(".content .title a, .cardWrap a");
  39. links.forEach((linkEl) => linkEl.setAttribute("href", url));
  40. el.setAttribute(attrFixedFlag, true);
  41. }
  42. };
  43.  
  44. // Fix when links added.
  45.  
  46. onChanged(sel("#__next"), fix);
  47. fix();
  48.  
  49. // Fix when history state changed.
  50.  
  51. const pushState = history.pushState.bind(history);
  52. const replaceState = history.replaceState.bind(history);
  53. const locationChanged = () => {
  54. fix();
  55. };
  56.  
  57. history.pushState = (...args) => {
  58. pushState(...args);
  59. locationChanged();
  60. };
  61. history.replaceState = (...args) => {
  62. replaceState(...args);
  63. locationChanged();
  64. };
  65.  
  66. window.addEventListener("popstate", locationChanged);
  67. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址