Rtings Open Shopping Links in New Tab Only

Opens shopping links in new tabs on rtings.com without affecting the current tab

  1. // ==UserScript==
  2. // @name Rtings Open Shopping Links in New Tab Only
  3. // @namespace https://greasyfork.org/en/users/594496-divided-by
  4. // @author dividedby
  5. // @description Opens shopping links in new tabs on rtings.com without affecting the current tab
  6. // @version 1.1
  7. // @license GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  8. // @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=dividedbyerror@gmail.com&item_name=Rtings+Tab+Donation
  9. // @contributionAmount $1
  10. // @match https://www.rtings.com/*
  11. // run-at document-idle
  12.  
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. // Array of domain patterns to match
  19. const domainPatterns = [
  20. 'amazon.com',
  21. 'ebay.com',
  22. 'walmart.com',
  23. 'target.com',
  24. 'bestbuy.com',
  25. 'bhphotovideo.com',
  26. 'shop-links.co'
  27. ];
  28.  
  29. function handleClick(event) {
  30. const link = event.currentTarget;
  31. if (domainPatterns.some(pattern => link.href.includes(pattern))) {
  32. event.preventDefault();
  33. event.stopPropagation();
  34. window.open(link.href, '_blank', 'noopener,noreferrer');
  35. }
  36. }
  37.  
  38. function enhanceLinks() {
  39. const selector = domainPatterns.map(pattern => `a[href*="${pattern}"]`).join(',');
  40. const links = document.querySelectorAll(`${selector}:not([data-enhanced])`);
  41. links.forEach(link => {
  42. link.setAttribute('data-enhanced', 'true');
  43. link.addEventListener('click', handleClick, true);
  44. });
  45. }
  46.  
  47. enhanceLinks();
  48.  
  49. const observer = new MutationObserver(mutations => {
  50. if (mutations.some(mutation => mutation.addedNodes.length > 0)) {
  51. enhanceLinks();
  52. }
  53. });
  54. observer.observe(document.body, { childList: true, subtree: true });
  55. })();
  56.