Original Content

Automatically redirect content stealing websites to their original counterparts

  1. // ==UserScript==
  2. // @name Original Content
  3. // @namespace http://xmine128.tk/gm/original-content/
  4. // @description Automatically redirect content stealing websites to their original counterparts
  5. // @require https://cdnjs.cloudflare.com/ajax/libs/xregexp/2.0.0/xregexp-min.js
  6. // @license GPL-3.0+
  7. // @include *://*.bigresource.com/*
  8. // @include *://*.techforumnetwork.com/*
  9. // @include *://techforumnetwork.com/*
  10. // @include *://*.linuxine.com/story/*
  11. // @include *://linuxine.com/story/*
  12. // @include *://*.forumsee.com/*
  13. // @include *://forumsee.com/*
  14. // @include *://*.bighow.net/*
  15. // @include *://bighow.net/*
  16. // @include *://*.techassistbox.com/*
  17. // @include *://techassistbox.com/*
  18. // @include *://*.youku.io/*
  19. // @include *://youku.io/*
  20. // @icon http://xmine128.tk/gm/original-content/icon.jpg
  21. // @version 1.1.3
  22. // @grant GM_xmlhttpRequest
  23. // @run-at document-start
  24. // ==/UserScript==
  25.  
  26. 'use strict';
  27.  
  28. /**
  29. * The icon has been released by the user "Nisha A" on Flickr under the CC-BY license
  30. *
  31. * Image on Flickr: https://www.flickr.com/photos/samiksha/445070705/sizes/o/in/photostream/
  32. * Created by user: https://www.flickr.com/photos/samiksha/
  33. */
  34. const URL = window.location.href;
  35.  
  36. /**
  37. * Call the given callback function at the given time during page load
  38. *
  39. * Note: This function assumes that it is being called during "document-start"!
  40. *
  41. * @param {Function} callback
  42. * The function to call
  43. * @param {String} run_at
  44. * During what stage of the page load the function should be called ("document-start", "document-end" or "load")
  45. * @param {Object} ...
  46. * Additional parameters to pass to the callback function
  47. */
  48. function _callback_run_at(callback, run_at)
  49. {
  50. // Read the additonal parameters
  51. var params = Array.prototype.slice.call(arguments, 2);
  52. switch(run_at) {
  53. case 'document-start':
  54. Function.prototype.apply.call(callback, null, params);
  55. break;
  56. case 'load':
  57. window.addEventListener("load", function()
  58. {
  59. Function.prototype.apply.call(callback, null, params);
  60. }, false);
  61. break;
  62. default:
  63. document.addEventListener("DOMContentLoaded", function()
  64. {
  65. Function.prototype.apply.call(callback, null, params);
  66. }, false);
  67. }
  68. }
  69.  
  70.  
  71. /**
  72. * Process a single URL rewrite rule
  73. *
  74. * If `rewriter` is a function then it will receive the following parameters:
  75. * - URL: The URL of the current page
  76. * - result: The result of the regular expression matching operation
  77. *
  78. * @param {RegExp|String} regexp
  79. * The regular expression that must be match by the current page URL
  80. * @param {Function|String} rewriter
  81. * A function or XRegExp rewrite string used for replacing the URL
  82. */
  83. function rule_url(regexp, rewriter)
  84. {
  85. var result = URL.match(regexp);
  86. if(result) {
  87. var href = null;
  88. if(typeof(rewriter) == 'function') {
  89. href = rewriter(URL, result);
  90. } else {
  91. // Rewrite URL using XRegExp :-)
  92. href = XRegExp.replace(URL, regexp, rewriter);
  93. }
  94. // Prevent page load
  95. //TODO: Get this to do anything more blanking the page
  96. var interval = window.setInterval(function()
  97. {
  98. document.documentElement.innerHTML = "";
  99. }, 1);
  100. document.addEventListener("DOMContentLoaded", function(event)
  101. {
  102. clearInterval(interval);
  103. });
  104. // Rewrite URL
  105. window.location.replace(href);
  106. }
  107. }
  108.  
  109.  
  110. /**
  111. * Follow a hyperlink on a page
  112. *
  113. * @param {RegExp|String} regexp
  114. * The regular expression that must be match by the current page URL
  115. * @param {Function|String} selector
  116. * A function or XRegExp rewrite string used to generated to selector of the element to click on
  117. * @param {String} [run_at="document-end"]
  118. * When to perform the action ("document-start", "document-end" or "load")
  119. */
  120. function rule_link(regexp, selector, run_at)
  121. {
  122. var result = URL.match(regexp);
  123. if(result) {
  124. if(typeof(selector) == 'function') {
  125. selector = selector(URL, result);
  126. } else {
  127. // Rewrite URL using XRegExp
  128. selector = XRegExp.replace(URL, regexp, selector);
  129. }
  130. // Click at the correct stage during page load
  131. _callback_run_at(function(selector)
  132. {
  133. window.location.replace(document.querySelector(selector).href);
  134. }, (run_at || "document-end"), selector);
  135. }
  136. }
  137.  
  138.  
  139. /**
  140. * Process a single URL action rule
  141. *
  142. * If `callback` is a function then it will receive the following parameters:
  143. * - URL: The URL of the current page
  144. * - result: The result of the regular expression matching operation
  145. * If `callback` is a string then that string will be executed in the page's context
  146. *
  147. * @param {RegExp|String} regexp
  148. * The regular expression that must be match by the current page URL
  149. * @param {Function|String} callback(URL)
  150. * The function to call when the page URL matches the given regex
  151. * @param {String} [run_at="document-end"]
  152. * When to perform the action ("document-start", "document-end" or "load")
  153. */
  154. function rule_action(regexp, callback, run_at)
  155. {
  156. var result = URL.match(regexp);
  157. if(result) {
  158. // Create wrapper function for string callback
  159. if(typeof(callback) == "string") {
  160. var code = callback;
  161. callback = function()
  162. {
  163. window.location.href = "javascript:" + code;
  164. }
  165. }
  166. // Execute callback function at the correct stage during page load
  167. _callback_run_at(callback, (run_at || "document-end"), URL, result);
  168. }
  169. }
  170.  
  171. rule_url(/^http[s]?:\/\/([^.]+)\.bigresource\.com\/(?:[^-]+[-])+[-]?([A-Za-z0-9]{9,})\.html$/i, "http://$1.bigresource.com/Track/$2/");
  172. rule_url(/^http[s]?:\/\/(?:[^.]+\.)*bighow\.net\/(\d+)-([^.]+)\.html$/i, "http://bighow.net/track/$1/$2");
  173.  
  174. rule_link(/^http[s]?:\/\/(?:[^.]+\.)*linuxine\.com\/story\/.+$/i, "a.pviewlink");
  175. rule_link(/^http[s]?:\/\/(?:[^.]+\.)*forumsee\.com\/.+$/i, "a.bigLink");
  176. rule_link(/^http[s]?:\/\/(?:[^.]+\.)*youku\.io\/.+$/i, "#a_text > .pub_info > a");
  177.  
  178. rule_action(/^http[s]?:\/\/(?:[^.]+\.)*techforumnetwork\.com\/techqns\/[a-z0-9-]+\/$/i, "readPost()", "document-end");
  179. rule_action(/^http[s]?:\/\/(?:[^.]+\.)*techassistbox\.com\/[A-Za-z0-9-]+_\d+[.]html$/i, function()
  180. {
  181. var id = document.querySelector("#answerQuestion > button").onclick.toString().match(/v_th\(['"]([A-Za-z0-9]+)['"]\)/i)[1];
  182. window.location.replace("http://www.techassistbox.com/goto/" + id + "/");
  183. });