Private tabs for certain links

Opens certain links in a private FF tab. Requires https://addons.mozilla.org/fr/firefox/addon/private-tab/?src=api

  1. // ==UserScript==
  2. // @name Private tabs for certain links
  3. // @description Opens certain links in a private FF tab. Requires https://addons.mozilla.org/fr/firefox/addon/private-tab/?src=api
  4. // @namespace http://userscripts.org/users/399688
  5. // @include /https?:\/\/(np\.|www\.)reddit.com\/.*/
  6. // @version 1.1.1
  7. // @grant GM_openInTab
  8. // ==/UserScript==
  9. /*
  10. Changelog:
  11. v2.0.0
  12. Allows choosing the mode of operation from either inclusive or exclusive site filtering.
  13. Inclusive = Only open these sites in private tabs
  14. Exclusive = Open all sites in private tabs excluding the ones you define
  15.  
  16. v1.1.1
  17. MObile youtube m.youtube.com
  18.  
  19. v1.1.0
  20. Clicking on elements in anchors work now
  21.  
  22. v1.0.0
  23. Initial version for anchors
  24. */
  25.  
  26. /**
  27. Do we want to INCLUDE only certain sites for privacy?
  28. Otherwise (false) every site is private EXCLUDING certain sites
  29. */
  30. var inclusiveScript = false;
  31.  
  32. /**
  33. Site that should be included for privacy
  34. */
  35. var inclusiveRegexs = [
  36. /^https?:\/\/((www|m)\.)?(youtu\.?be)/
  37. ]
  38. /**
  39. Sites that should be EXCLUDED from privacy
  40. */
  41. var exclusiveRegexs = [
  42. /^javascript:.*/,
  43. /https?:\/\/(np\.|www\.)reddit.com\/.*/
  44. ]
  45.  
  46.  
  47. var openInBackground = false;
  48.  
  49. /**
  50. Gets the first element in the heirarchy that has the given tagname
  51. */
  52. function getFirstParentOftag(element, tagName) {
  53. if (element.tagName == tagName) {
  54. return element;
  55. }
  56. var parent = element.parentElement;
  57. if (parent) {
  58. return getFirstParentOftag(parent, tagName);
  59. }
  60. return null;
  61. }
  62.  
  63. /**
  64. First class function to call a method with given args on a future given object
  65. */
  66. function callWith(method, ...args) {
  67. return function (o) {
  68. var func = o[method];
  69. if (typeof func !== 'function') {
  70. message = method + ' is not a function';
  71. console.error(message + ' of ', o);
  72. throw new TypeError(message);
  73. }
  74. return func.apply(o, args);
  75. }
  76. }
  77.  
  78. /**
  79. Should we open the page in a private tab?
  80. */
  81. function shouldPrivatise(href){
  82. if(inclusiveScript){
  83. return inclusiveRegexs.some(callWith('test', href));
  84. } else {
  85. return ! exclusiveRegexs.some(callWith('test', href));
  86. }
  87. }
  88.  
  89. /**
  90. Opens the link in a private tab
  91. */
  92. function handler(event) {
  93. // Only left clicks
  94. if (event.button !== 0) {
  95. return;
  96. }
  97. // Our target might be wrapped in an <a>
  98. var firstA = getFirstParentOftag(event.target, 'A');
  99. if (!firstA) {
  100. return;
  101. }
  102. // Only ourlinks we wanna privatise
  103. var href = firstA.href;
  104. if (shouldPrivatise(href)) {
  105. var privateHref = 'private:' + href;
  106. GM_openInTab(privateHref, openInBackground);
  107. event.preventDefault();
  108. }
  109. }
  110. document.addEventListener('click', handler, true);