eBay copy short url button

Adds a copy short url button to ebay item page. Copies a nice link instead of a long one. The short url - http://www.ebay.com/itm/item_id

目前為 2018-06-25 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name eBay copy short url button
  3. // @namespace https://github.com/arieljannai
  4. // @version 0.1.1
  5. // @description Adds a copy short url button to ebay item page. Copies a nice link instead of a long one. The short url - http://www.ebay.com/itm/item_id
  6. // @author Ariel Jannai
  7. // @include *://www.ebay.com/itm/*
  8. // @include *://www.ebay.de/itm/*
  9. // @include *://www.ebay.co.uk/itm/*
  10. // @include *://www.ebay.com.au/itm/*
  11. // @run-at document-end
  12. // @icon https://cdn4.iconfinder.com/data/icons/flat-brand-logo-2/512/ebay-128.png
  13. // @grant none
  14. // @license MIT
  15. // ==/UserScript==
  16. /* jshint -W097 */
  17. 'use strict';
  18.  
  19. // http://icons.iconarchive.com/icons/icons8/windows-8/32/Editing-Copy-Link-icon.png
  20. var copyIcon = 'url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAC0klEQVR42sWXW4hOURTH1+cyuUWiaYrShEmuzaN5kCFvkgcPmDFNUUpRDNM0pmaIkEKjKPdLuecSJS+SEo2HmUEeJJd4kEJKDTLD/99aX9+Z851zvn2+M2es+rX3Od/Z3/7vvc5ae52M5Gwt2Ahmg6ESbr3gLtgAPkhCy1h7DqyJOfYzWAqeJhXAlZ+w60/gIvgeMaYETAWvwFfQDvqSCOgC82zySmsL2QxQ4dnBIPsFHoCfhQT8EfX5QbDFYeJTYL7jAj+CGvAwSsBf67eBHRF/Ng08ARMcJ8/ab1ALriYVcFv0paMdFX1xeyKeHw+uW8vIqQMXihUwFnwDQ+xPahxXfwsss36gCFcBc0G39VeLRoqLnQb1nus8Ea4CGCVd1l9uKytGQJ6I/yEgK2IFuJm2gD2gKeQ3vsBVaQsYDpZYSxsDykAjKAWP0hYQZrvAdnbSFsAcwBB+77vfavOlJmAkOCYasswdz2zc28EQwMnvgEXgEngNtoHHoHowBEwBM8EkyR3zZ0XPg2yhk4qAYaJFTZWoz4+I1gujQKdoHVGelgBuOw+rxaLFCf3eARZ63FFvOzHgArw+3w+aRWN/Mlhp95mM9nnGDJgA7+SHwDvRg4qplud/dcDkeQL6rKX6xpgCuKU8WPaC8+C5aIVFKwmZPE/ACzBLtMSeI8EFaZAAVkasjK+AVfYbV1xri+JO3A9ZUD8B/BZotx/eiNZ8PzwPv7SJ/ALK7fndoEU04zEKOmxRUdVSPwF8Y29IrnLx2xnzb5ALmOFYHV8WLVSnR2x7qAAaE0QD2CSaQFwFcBeOi4YbY3+n5EItlgCvTQSjPdd0R6m5grZeNM8nsQNgM+jNOA5glvsi6mfuBLNdj+NYv7Ee6LS221UAjWG61fo8YK5Jga+eABsnGjFldr0ujoAR4B5YUOTK/ca8URdHAI2lFX3HD9qKIiZlhmSIHgYneeMfv6reNvMbmkwAAAAASUVORK5CYII=")';
  21. var copyButton = '<td><div class="copy-url"><a rel="nofollow" href="javascript:;" ' +
  22. 'class="scIcon copy-short-url" aria-label="Copy short url to clipboard" ' +
  23. 'title="Copy short url to clipboard" target="_blank"></a></div></td>';
  24.  
  25. addGlobalStyle('.copy-short-url { background: ' + copyIcon + '; width: 18px; height: 18px; float: left; background-size: contain;}');
  26. addGlobalStyle('.copy-url { margin-right: 3px; }');
  27.  
  28. $('div.share table tbody tr').prepend(copyButton);
  29.  
  30.  
  31. $('.copy-url')[0].addEventListener('click', function(event) {
  32. copyTextToClipboard(tryShortUrl(document.location.href));
  33. });
  34.  
  35. function tryShortUrl(url) {
  36. var urlPattern = /ebay\.(com|de|co\.uk)\/itm/;
  37.  
  38. if(urlPattern.test(url)) {
  39. var id = url.match(/\/(\d{12})(\?|$)/)[1];
  40. return 'http://www.' + urlPattern.exec(url)[0] + '/' + id;
  41. } else {
  42. return url;
  43. }
  44. }
  45.  
  46. // https://somethingididnotknow.wordpress.com/2013/07/01/change-page-styles-with-greasemonkeytampermonkey/
  47. function addGlobalStyle(css) {
  48. var head, style;
  49. head = document.getElementsByTagName('head')[0];
  50. if (!head) { return; }
  51. style = document.createElement('style');
  52. style.type = 'text/css';
  53. style.innerHTML = css;
  54. head.appendChild(style);
  55. }
  56.  
  57. // http://stackoverflow.com/a/30810322/2350423
  58. function copyTextToClipboard(text) {
  59. var textArea = document.createElement("textarea");
  60.  
  61. //
  62. // *** This styling is an extra step which is likely not required. ***
  63. //
  64. // Why is it here? To ensure:
  65. // 1. the element is able to have focus and selection.
  66. // 2. if element was to flash render it has minimal visual impact.
  67. // 3. less flakyness with selection and copying which **might** occur if
  68. // the textarea element is not visible.
  69. //
  70. // The likelihood is the element won't even render, not even a flash,
  71. // so some of these are just precautions. However in IE the element
  72. // is visible whilst the popup box asking the user for permission for
  73. // the web page to copy to the clipboard.
  74. //
  75.  
  76. // Place in top-left corner of screen regardless of scroll position.
  77. textArea.style.position = 'fixed';
  78. textArea.style.top = 0;
  79. textArea.style.left = 0;
  80.  
  81. // Ensure it has a small width and height. Setting to 1px / 1em
  82. // doesn't work as this gives a negative w/h on some browsers.
  83. textArea.style.width = '2em';
  84. textArea.style.height = '2em';
  85.  
  86. // We don't need padding, reducing the size if it does flash render.
  87. textArea.style.padding = 0;
  88.  
  89. // Clean up any borders.
  90. textArea.style.border = 'none';
  91. textArea.style.outline = 'none';
  92. textArea.style.boxShadow = 'none';
  93.  
  94. // Avoid flash of white box if rendered for any reason.
  95. textArea.style.background = 'transparent';
  96.  
  97.  
  98. textArea.value = text;
  99.  
  100. document.body.appendChild(textArea);
  101.  
  102. textArea.select();
  103.  
  104. try {
  105. var successful = document.execCommand('copy');
  106. var msg = successful ? 'successful' : 'unsuccessful';
  107. console.log('Copying text command was ' + msg + ': ' + text);
  108. } catch (err) {
  109. console.log('Oops, unable to copy');
  110. }
  111.  
  112. document.body.removeChild(textArea);
  113. }