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

目前為 2016-03-03 提交的版本,檢視 最新版本

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