Greasy Fork is available in English.

Remove User ID from Stack Overflow Share URLs

Removes the User ID from the URL displayed by the "Share"

Verze ze dne 09. 09. 2018. Zobrazit nejnovější verzi.

  1. // ==UserScript==
  2. // @name Remove User ID from Stack Overflow Share URLs
  3. // @namespace https://kevinlocke.name/userscripts
  4. // @description Removes the User ID from the URL displayed by the "Share"
  5. // button for questions and answers on Stack Overflow and related sites. For
  6. // a description of why the User ID was added, see
  7. // https://meta.stackoverflow.com/q/277769
  8. // @match https://*.askubuntu.com/*
  9. // @match https://*.mathoverflow.net/*
  10. // @match https://*.serverfault.com/*
  11. // @match https://*.stackapps.com/*
  12. // @match https://*.stackexchange.com/*
  13. // @match https://*.stackoverflow.com/*
  14. // @match https://*.superuser.com/*
  15. // @version 1.0.0
  16. // @license MIT
  17. // @grant none
  18. // @supportURL https://github.com/kevinoid/stackoverflow-share-remove-user-id
  19. // ==/UserScript==
  20.  
  21. // Note: Domain list from https://stackexchange.com/sites?view=list
  22.  
  23. 'use strict';
  24.  
  25. // Amount of information to log to the console. Lower = more.
  26. const logLevel = 3;
  27.  
  28. // Simple logging framework
  29. function notLogged() {}
  30. const log = {
  31. /* eslint-disable no-console */
  32. error: logLevel < 5 ? console.error : notLogged,
  33. warn: logLevel < 4 ? console.warn : notLogged,
  34. info: logLevel < 3 ? console.info : notLogged,
  35. log: logLevel < 2 ? console.log : notLogged,
  36. debug: logLevel < 1 ? (console.debug || console.log) : notLogged
  37. /* eslint-enable no-console */
  38. };
  39.  
  40. function forEach(arrayLike, callback, thisArg) {
  41. Array.prototype.forEach.call(arrayLike, callback, thisArg);
  42. }
  43.  
  44. /** Removes the User ID from the URL displayed in response to clicking a
  45. * "Share" link.
  46. */
  47. function removeUserIDOnClick(evt) {
  48. if (!evt.target.classList.contains('short-link')) {
  49. // click was not on "Share" link
  50. log.debug('Ignoring click not on .short-link.');
  51. return;
  52. }
  53.  
  54. const inputs = evt.target.parentNode.getElementsByTagName('input');
  55. if (inputs.length === 0) {
  56. log.warn('Could not find "Share" URL input element.');
  57. return;
  58. }
  59.  
  60. forEach(inputs, (input) => {
  61. // Only change URLs with known formats to avoid breaking other URL formats
  62. const oldUrl = input.value;
  63. const newUrl
  64. = oldUrl.replace(/^(https?:\/\/[^/]*\/[aq]\/[0-9]+)\/[0-9]+$/, '$1');
  65. if (newUrl === oldUrl) {
  66. if (/^https?:\/\/[^/]*\/[aq]\/[0-9]+$/.test(oldUrl)) {
  67. log.debug(`Ignoring "Share" URL without User ID: ${oldUrl}`);
  68. } else {
  69. log.warn(`Ignoring unrecognized "Share" URL: ${oldUrl}`);
  70. }
  71.  
  72. return;
  73. }
  74.  
  75. log.debug(`Changed "Share" URL from ${oldUrl} to ${newUrl}`);
  76. input.value = newUrl;
  77.  
  78. // Changing the value clears the selection. Select the URL text.
  79. input.setSelectionRange(0, newUrl.length);
  80. });
  81. }
  82.  
  83. // Wait until load event to register click event listener so that it is
  84. // registered (and therefore runs) after the Stack Overflow click listener.
  85. window.addEventListener(
  86. 'load',
  87. () => document.addEventListener('click', removeUserIDOnClick, false),
  88. false
  89. );