Greasy Fork is available in English.

Amazon short URL

Replace article URL with short Amazon permalink

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