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" button for questions and answers on Stack Overflow and related sites. For a description of why the User ID was added, see https://meta.stackoverflow.com/q/277769

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