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

  1. // ==UserScript==
  2. // @name eBay copy short url button
  3. // @namespace https://github.com/arieljannai
  4. // @version 0.1.4
  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 = '<div style="float:left"><a rel="nofollow" href="javascript:;" ' +
  22. 'class="copy-url" aria-label="Copy short url to clipboard" ' +
  23. 'title="Copy short url to clipboard" target="_blank"><span class="copy-short-url"/></a></div>';
  24.  
  25. function tryShortUrl(url) {
  26. var urlPattern = /ebay\.(com|de|co\.uk)\/itm/;
  27.  
  28. if(urlPattern.test(url)) {
  29. var id = url.match(/\/(\d{12})(\?|$)/)[1];
  30. return 'http://www.' + urlPattern.exec(url)[0] + '/' + id;
  31. } else {
  32. return url;
  33. }
  34. }
  35.  
  36. // https://somethingididnotknow.wordpress.com/2013/07/01/change-page-styles-with-greasemonkeytampermonkey/
  37. function addGlobalStyle(css) {
  38. var head, style;
  39. head = document.getElementsByTagName('head')[0];
  40. if (!head) { return; }
  41. style = document.createElement('style');
  42. style.type = 'text/css';
  43. style.innerHTML = css;
  44. head.appendChild(style);
  45. }
  46.  
  47. // http://stackoverflow.com/a/30810322/2350423
  48. function copyTextToClipboard(text) {
  49. var textArea = document.createElement("textarea");
  50.  
  51. //
  52. // *** This styling is an extra step which is likely not required. ***
  53. //
  54. // Why is it here? To ensure:
  55. // 1. the element is able to have focus and selection.
  56. // 2. if element was to flash render it has minimal visual impact.
  57. // 3. less flakyness with selection and copying which **might** occur if
  58. // the textarea element is not visible.
  59. //
  60. // The likelihood is the element won't even render, not even a flash,
  61. // so some of these are just precautions. However in IE the element
  62. // is visible whilst the popup box asking the user for permission for
  63. // the web page to copy to the clipboard.
  64. //
  65.  
  66. // Place in top-left corner of screen regardless of scroll position.
  67. textArea.style.position = 'fixed';
  68. textArea.style.top = 0;
  69. textArea.style.left = 0;
  70.  
  71. // Ensure it has a small width and height. Setting to 1px / 1em
  72. // doesn't work as this gives a negative w/h on some browsers.
  73. textArea.style.width = '2em';
  74. textArea.style.height = '2em';
  75.  
  76. // We don't need padding, reducing the size if it does flash render.
  77. textArea.style.padding = 0;
  78.  
  79. // Clean up any borders.
  80. textArea.style.border = 'none';
  81. textArea.style.outline = 'none';
  82. textArea.style.boxShadow = 'none';
  83.  
  84. // Avoid flash of white box if rendered for any reason.
  85. textArea.style.background = 'transparent';
  86.  
  87.  
  88. textArea.value = text;
  89.  
  90. document.body.appendChild(textArea);
  91.  
  92. textArea.select();
  93.  
  94. try {
  95. var successful = document.execCommand('copy');
  96. var msg = successful ? 'successful' : 'unsuccessful';
  97. console.log('Copying text command was ' + msg + ': ' + text);
  98. } catch (err) {
  99. console.log('Oops, unable to copy');
  100. }
  101.  
  102. document.body.removeChild(textArea);
  103. }
  104.  
  105. // start
  106.  
  107. addGlobalStyle('.copy-short-url { background: ' + copyIcon + '; width: 18px; height: 18px; float: left; background-size: contain;}');
  108. addGlobalStyle('.copy-url { margin-right: 3px; }');
  109.  
  110. $('div.social-widget > div')[0].style.float = "right";
  111. $('div.social-widget').prepend(copyButton);
  112.  
  113. $('.copy-url')[0].addEventListener('click', function(event) {
  114. copyTextToClipboard(tryShortUrl(document.location.href));
  115. });