Amiami跳转链接按钮

在Amiami商品页右下角添加一个实现amiami.jp与amiami.com互相跳转的链接按钮。

  1. // ==UserScript==
  2. // @name Amiami Redirect Link Button
  3. // @name:zh-CN Amiami跳转链接按钮
  4. // @namespace https://greasyfork.org/zh-CN/scripts/521740
  5. // @version 0.6
  6. // @description Add buttons to redirect between Amiami.jp and Amiami.com.
  7. // @description:zh-cn 在Amiami商品页右下角添加一个实现amiami.jp与amiami.com互相跳转的链接按钮。
  8. // @author Mr_Ebonycat
  9. // @match https://www.amiami.jp/top/detail/detail?scode=*
  10. // @match https://www.amiami.com/*
  11. // @grant none
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // 提取URL中的scode或gcode参数
  19. function extractCode(url) {
  20. const urlObj = new URL(url);
  21. const params = urlObj.searchParams;
  22. return params.get('scode') || params.get('gcode');
  23. }
  24.  
  25. // 创建跳转按钮
  26. function createRedirectButton() {
  27. const currentUrl = window.location.href;
  28. const code = extractCode(currentUrl);
  29.  
  30. if (!code) {
  31. console.error('无法从URL中提取scode或gcode参数');
  32. return null;
  33. }
  34.  
  35. const redirectUrl = createRedirectUrl(currentUrl, code);
  36. if (!redirectUrl) {
  37. return null;
  38. }
  39.  
  40. const redirectButton = document.createElement('a');
  41. redirectButton.href = redirectUrl; // 设置链接地址
  42. redirectButton.textContent = currentUrl.includes('amiami.com') ? 'Go to JP' : 'Go to COM';
  43. redirectButton.id = 'amiami-redirect-button';
  44. redirectButton.style.position = 'fixed';
  45. redirectButton.style.bottom = '20px';
  46. redirectButton.style.right = '20px';
  47. redirectButton.style.zIndex = '9999';
  48. redirectButton.style.padding = '2px 4px'; // 修改按钮内边距为2px 4px
  49. redirectButton.style.border = 'none';
  50. redirectButton.style.backgroundColor = '#4CAF50';
  51. redirectButton.style.color = 'white';
  52. redirectButton.style.borderRadius = '5px';
  53. redirectButton.style.cursor = 'pointer';
  54. redirectButton.style.display = 'inline-block'; // 使链接表现得像按钮
  55. redirectButton.style.textDecoration = 'none'; // 去除超链接的下划线
  56.  
  57. return redirectButton;
  58. }
  59.  
  60. // 创建跳转URL
  61. function createRedirectUrl(currentUrl, code) {
  62. if (currentUrl.includes('amiami.jp')) {
  63. // 如果当前在amiami.jp页面,跳转到amiami.com的detail页面
  64. return `https://www.amiami.com/cn/detail?gcode=${code}`;
  65. } else if (currentUrl.includes('amiami.com')) {
  66. // 如果当前在amiami.com页面,跳转到amiami.jp的top/detail页面
  67. return `https://www.amiami.jp/top/detail/detail?scode=${code}`;
  68. }
  69. return null;
  70. }
  71.  
  72. // 添加按钮到页面中
  73. function addButtonToPage() {
  74. const existingButton = document.getElementById('amiami-redirect-button');
  75. if (existingButton) {
  76. existingButton.remove();
  77. }
  78. const redirectButton = createRedirectButton();
  79. if (redirectButton) {
  80. document.body.appendChild(redirectButton);
  81. }
  82. }
  83.  
  84. // 初始化脚本
  85. function initialize() {
  86. const pathname = window.location.pathname;
  87. if (pathname.includes('/detail')) {
  88. addButtonToPage();
  89. }
  90. }
  91.  
  92. // 监听URL变化
  93. function observeUrlChanges() {
  94. let lastUrl = window.location.href;
  95. const observer = new MutationObserver(() => {
  96. const currentUrl = window.location.href;
  97. if (currentUrl !== lastUrl) {
  98. lastUrl = currentUrl;
  99. initialize();
  100. }
  101. });
  102. observer.observe(document.body, { childList: true, subtree: true });
  103. }
  104.  
  105. window.addEventListener('load', () => {
  106. initialize();
  107. observeUrlChanges();
  108. });
  109. })();