Fix New-Window Links (Remove target Attribute)

Remove the target attribute from the links matching the given string.

  1. // ==UserScript==
  2. // @name Fix New-Window Links (Remove target Attribute)
  3. // @namespace https://github.com/theborg3of5/Userscripts/
  4. // @version 1.0
  5. // @description Remove the target attribute from the links matching the given string.
  6. // @author Gavin Borg
  7. // @require https://greasyfork.org/scripts/28536-gm-config/code/GM_config.js?version=184529
  8. // @match https://greasyfork.org/en/scripts/404695-fix-new-window-links-remove-target-attribute
  9. // @grant GM_registerMenuCommand
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // ==/UserScript==
  13.  
  14. var config = GM_config;
  15. var maxNumSelectors = 5; // How many different selectors are configurable per site.
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. var site = getMatchingSite();
  21. initConfig(site);
  22.  
  23. var siteClean = cleanSite(site);
  24. for (var i = 1; i <= maxNumSelectors; i++) {
  25. "selector" + i
  26. var selector = config.get(getConfigField(i));
  27. if(selector) {
  28. fixLinksMatchingQuery(selector);
  29. }
  30. }
  31. })();
  32.  
  33. function getMatchingSite() {
  34. // Get sites that user has chosen to include or match (because that's what hotkeys are keyed to, not direct URLs)
  35. var sites = GM_info.script.options.override.use_matches;
  36. sites.concat(GM_info.script.options.override.use_includes);
  37.  
  38. // Find matching site
  39. var currentURL = window.location.href;
  40. for (var site of sites) {
  41. // Use a RegExp to determine which of the user's includes/matches is currently open, since we allow different hotkeys/anchors per each of those.
  42. var siteRegex = new RegExp(site.replace(/\*/g, "[^ ]*")); // Replace * wildcards with regex-style [^ ]* wildcards
  43. if (siteRegex.test(currentURL)) {
  44. return site; // First match always wins
  45. }
  46. }
  47. }
  48.  
  49. function initConfig(site) {
  50. var siteClean = cleanSite(site);
  51.  
  52. // Build the link selector fields
  53. var fields = {};
  54. for (var i = 1; i <= maxNumSelectors; i++) {
  55. fields[getConfigField(i)] = {
  56. label: "Selector to fix (jQuery-style) #" + i + ":",
  57. title: "The jQuery-style selector that defines the link element(s) that you want to fix.",
  58. type: "text"
  59. };
  60. }
  61.  
  62. config.init({
  63. id: "FixNewWindowLinksConfig" + siteClean,
  64. title: "Fix New Window Links Config for: " + site,
  65. fields: fields,
  66. events: {
  67. 'save': function() { config.close(); }
  68. }
  69. });
  70.  
  71. // Add a menu item to the menu to launch the config
  72. GM_registerMenuCommand("Configure links to fix for this site", () => {
  73. config.open();
  74. })
  75. }
  76.  
  77. function cleanSite(site) {
  78. return site.replace(/[\*/:\?\.]/g, ""); // Drop */:?. characters from site for use in ID
  79. }
  80.  
  81. function getConfigField(index) {
  82. return "selector" + index;
  83. }
  84.  
  85. function fixLinksMatchingQuery(queryString) {
  86. if (document.querySelector(queryString)) { // Make sure the links we want to fix exist, in case they show up via AJAX a little later than page load.
  87. document.querySelectorAll(queryString).forEach(link => link.removeAttribute("target"));
  88. } else {
  89. setTimeout(fixLinksMatchingQuery.bind(null, queryString), 0);
  90. }
  91. }