Pandabuy Redirector

Redirects Weidian and Taobao links to Pandabuy

  1. // ==UserScript==
  2. // @name Pandabuy Redirector
  3. // @namespace http://tampermonkey
  4. // @version 3.1
  5. // @description Redirects Weidian and Taobao links to Pandabuy
  6. // @match *://*/*
  7. // @grant none
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. "use strict";
  13. // Define constants for Pandabuy, Weidian, and Taobao domains
  14. const pandabuyDomain = "pandabuy.com";
  15. const weidianDomain = "weidian.com";
  16. const taobaoDomain = "taobao.com";
  17.  
  18. // Function to check if a link matches Pandabuy, Weidian, or Taobao domains
  19. function checkLink(link) {
  20. if (
  21. link.href.includes(pandabuyDomain) ||
  22. link.href.includes(weidianDomain) ||
  23. link.href.includes(taobaoDomain)
  24. ) {
  25. link.addEventListener("click", function (event) {
  26. event.preventDefault();
  27. // If the link is not on Pandabuy, redirect to Pandabuy
  28. if (!link.href.includes(pandabuyDomain)) {
  29. // If the link is a Weidian link with a userid parameter, construct a new URL with the Pandabuy shop detail page and userid as a parameter
  30. if (
  31. link.href.includes(weidianDomain) &&
  32. link.href.includes("userid")
  33. ) {
  34. const userid = link.href.match(/userid=(\d+)/)[1];
  35. const newUrl =
  36. "https://www.pandabuy.com/shopdetail?t=wd&id=" +
  37. encodeURIComponent(userid);
  38. const newLink = document.createElement("a");
  39. newLink.href = newUrl;
  40. newLink.target = "_blank";
  41. newLink.click();
  42. }
  43. // If the link is a Taobao link with a subdomain, check if it's "item" and redirect to the usual product page if it is, otherwise construct a new URL with the Pandabuy shop detail page and subdomain as a parameter
  44. else if (
  45. link.href.includes(taobaoDomain) &&
  46. link.href.includes("://") &&
  47. link.href.includes(".")
  48. ) {
  49. const subdomain = link.href.match(/\/\/(.+?)\./)[1];
  50. if (subdomain === "item" || subdomain === "m") {
  51. const newUrl =
  52. "https://www.pandabuy.com/product?url=" +
  53. encodeURIComponent(link.href);
  54. const newLink = document.createElement("a");
  55. newLink.href = newUrl;
  56. newLink.target = "_blank";
  57. newLink.click();
  58. } else {
  59. const newUrl =
  60. "https://www.pandabuy.com/shopdetail?t=taobao&id=" +
  61. encodeURIComponent(subdomain);
  62. const newLink = document.createElement("a");
  63. newLink.href = newUrl;
  64. newLink.target = "_blank";
  65. newLink.click();
  66. }
  67. }
  68. // If the link is not a Weidian or Taobao link, construct a new URL with the Pandabuy product page and the original link as a parameter
  69. else {
  70. const newUrl =
  71. "https://www.pandabuy.com/product?url=" +
  72. encodeURIComponent(link.href);
  73. const newLink = document.createElement("a");
  74. newLink.href = newUrl;
  75. newLink.target = "_blank";
  76. newLink.click();
  77. }
  78. }
  79. });
  80. }
  81. }
  82. // Check if user is on pandabuy.com, weidian.com, or taobao.com before applying the link conversion script
  83. if (
  84. !window.location.href.includes(pandabuyDomain) &&
  85. !window.location.href.includes(weidianDomain) &&
  86. !window.location.href.includes(taobaoDomain)
  87. ) {
  88. // Check existing links on page load
  89. const links = document.getElementsByTagName("a");
  90. for (let i = 0; i < links.length; i++) {
  91. checkLink(links[i]);
  92. }
  93. // Observe for changes in the DOM and check new links
  94. const observer = new MutationObserver(function (mutationsList, observer) {
  95. for (let mutation of mutationsList) {
  96. if (mutation.type === "childList") {
  97. for (let addedNode of mutation.addedNodes) {
  98. if (addedNode.nodeType === 1) {
  99. if (addedNode.tagName === "A") {
  100. checkLink(addedNode);
  101. }
  102. // Check links in any new nodes that are added to the DOM
  103. else {
  104. const containerLinks = addedNode.getElementsByTagName("a");
  105. for (let j = 0; j < containerLinks.length; j++) {
  106. checkLink(containerLinks[j]);
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. });
  114. observer.observe(document.documentElement, {
  115. childList: true,
  116. subtree: true,
  117. });
  118. }
  119. })();