Amazon short URL

Replace article URL with short Amazon permalink

当前为 2022-10-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Amazon short URL
  3. // @namespace graphen
  4. // @version 4.0.6
  5. // @description Replace article URL with short Amazon permalink
  6. // @author Graphen
  7. // @include /^https?:\/\/(www|smile)\.amazon\.(cn|in|sg|ae|fr|de|it|nl|es|ca|com(\.(mx|au|br|tr|be))?|co\.(uk|jp))\/.*(dp|gp\/(product|video)|exec\/obidos\/ASIN|o\/ASIN)\/.*$/
  8. // @icon https://www.amazon.com/favicon.ico
  9. // @noframes
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. /* jshint esversion: 6 */
  14. (function (doc) {
  15. 'use strict';
  16.  
  17. function getAsin(){
  18. let asinId = doc.getElementById('ASIN');
  19.  
  20. if (asinId) {
  21. return asinId.value;
  22. }
  23. else {
  24. // Get ASIN from canonical link
  25. let links = doc.getElementsByTagName('link');
  26.  
  27. let i;
  28. for (i=0; i < links.length; i++) {
  29.  
  30. if (links[i].rel === 'canonical') {
  31.  
  32. let canonical = links[i].href;
  33. let asin = canonical.replace(/https?:\/\/(www|smile)\.amazon\..*\/dp\/([\w]+)$/, '$2');
  34.  
  35. if (asin.length === 10) {
  36. return asin;
  37. }
  38. }
  39. }
  40. }
  41. }
  42.  
  43. function replaceUrl() {
  44. let asin = getAsin();
  45. if (asin){
  46. history.replaceState(null, 'Amazon URL Cleaner', '/dp/' + asin + '/');
  47. //console.log("URL replaced by Amazon URL Cleaner. ASIN: " + asin);
  48. }
  49. }
  50. replaceUrl();
  51.  
  52. // Execute again when item variation is detected
  53. var buyboxParent = doc.getElementById('desktop_buybox');
  54. if (buyboxParent) {
  55. var MO = new MutationObserver(function(mutations) {
  56. mutations.forEach(function(mutation) {
  57. mutation.addedNodes.forEach(function(nodeElement) {
  58. if (nodeElement.id === "buybox") {
  59. replaceUrl();
  60. }
  61. });
  62. });
  63. });
  64. MO.observe(buyboxParent, { childList: true, subtree: true });
  65. }
  66.  
  67. }) (document);