Advertisement
Guest User

Untitled

a guest
May 19th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. const storageGet = chrome.storage.local.get;
  2. const TimeoutDelay = 2 * 1000;
  3.  
  4. let allowInjection = true;
  5. let storedObjectPrefix = getRandomString();
  6. let storageElems;
  7. let notificationTimeoutID;
  8.  
  9.  
  10. if (window.frameElement != null && window.frameElement.sandbox != null) {
  11. allowInjection = false;
  12. for (let i = 0; i < window.frameElement.sandbox.length; i++) {
  13. const val = window.frameElement.sandbox[i];
  14. if (val == 'allow-scripts') {
  15. allowInjection = true;
  16. }
  17. }
  18. }
  19.  
  20. if (allowInjection) {
  21. storageGet(["docId", "data", "enabled", "whitelist"], (elems)=>{
  22. const {docId, data, enabled = true, whitelist = {}} = elems || {};
  23. if (inWhiteList(whitelist) || !data){
  24. return;
  25. }
  26. storageElems = elems;
  27. if (enabled) {
  28. overrideMethods(docId, JSON.parse(data));
  29. } else {
  30. restoreMethods(docId, JSON.parse(data));
  31. }
  32. });
  33. }
  34.  
  35. function inWhiteList(whitelist) {
  36. if (window.location.href in whitelist){
  37. return true
  38. }
  39. const {hostname, host} = window.location;
  40. if (hostname in whitelist || host in whitelist) {
  41. return true;
  42. }
  43. return false;
  44. }
  45.  
  46. function overrideMethods(docId, data) {
  47. const script = document.createElement('script');
  48. script.id = getRandomString();
  49. script.type = "text/javascript";
  50. if (allowInjection) {
  51. var newChild = document.createTextNode('try{(' + overrideDefaultMethods + ')(' + data.r + ',' + data.g + ',' + data.b + ',' + data.a + ',"' + script.id + '", "' + storedObjectPrefix + '");} catch (e) {console.error(e);}');
  52. script.appendChild(newChild);
  53. var node = (document.documentElement || document.head || document.body);
  54. if (typeof node[docId] === 'undefined') {
  55. node.insertBefore(script, node.firstChild);
  56. node[docId] = getRandomString();
  57. }
  58. }
  59. }
  60.  
  61. function restoreMethods(docId, data) {
  62. const script = document.createElement('script');
  63. script.id = getRandomString();
  64. script.type = "text/javascript";
  65. if (allowInjection) {
  66. var newChild = document.createTextNode('try{(' + restoreDefaultMethods + ')(' + data.r + ',' + data.g + ',' + data.b + ',' + data.a + ',"' + script.id + '", "' + storedObjectPrefix + '");} catch (e) {console.error(e);}');
  67. script.appendChild(newChild);
  68. var node = (document.documentElement || document.head || document.body);
  69. if (node[docId]) {
  70. node.insertBefore(script, node.firstChild);
  71. delete node[docId];
  72. }
  73. }
  74. }
  75.  
  76. function getRandomString() {
  77. var text = "";
  78. var charset = "abcdefghijklmnopqrstuvwxyz";
  79. for (var i = 0; i < 5; i++)
  80. text += charset.charAt(Math.floor(Math.random() * charset.length));
  81. return text;
  82. }
  83.  
  84. function showNotification() {
  85. console.log("showNotification");
  86. chrome.runtime.sendMessage({action: "show-notification", url: window.location.href});
  87. }
  88.  
  89. (function addBodyListener() {
  90. window.addEventListener(storedObjectPrefix + "_show_notification", (evt)=>{
  91. if (notificationTimeoutID) {
  92. clearTimeout(notificationTimeoutID);
  93. }
  94. notificationTimeoutID = setTimeout(showNotification, TimeoutDelay)
  95. });
  96. })();
  97.  
  98. function overrideDefaultMethods(r, g, b, a, scriptId, storedObjectPrefix) {
  99. var scriptNode = document.getElementById(scriptId);
  100. function showNotification() {
  101. const evt = new CustomEvent(storedObjectPrefix + "_show_notification", {'detail': {}});
  102. window.dispatchEvent(evt);
  103. }
  104. function overrideCanvasProto(root) {
  105. function overrideCanvasInternal(name, old) {
  106. root.prototype[storedObjectPrefix + name] = old;
  107. Object.defineProperty(root.prototype, name,
  108. {
  109. value: function () {
  110. var width = this.width;
  111. var height = this.height;
  112. var context = this.getContext("2d");
  113. var imageData = context.getImageData(0, 0, width, height);
  114. for (var i = 0; i < height; i++) {
  115. for (var j = 0; j < width; j++) {
  116. var index = ((i * (width * 4)) + (j * 4));
  117. imageData.data[index + 0] = imageData.data[index + 0] + r;
  118. imageData.data[index + 1] = imageData.data[index + 1] + g;
  119. imageData.data[index + 2] = imageData.data[index + 2] + b;
  120. imageData.data[index + 3] = imageData.data[index + 3] + a;
  121. }
  122. }
  123. context.putImageData(imageData, 0, 0);
  124. showNotification();
  125. return old.apply(this, arguments);
  126. }
  127. }
  128. );
  129. }
  130. overrideCanvasInternal("toDataURL", root.prototype.toDataURL);
  131. overrideCanvasInternal("toBlob", root.prototype.toBlob);
  132. //overrideCanvasInternal("mozGetAsFile", root.prototype.mozGetAsFile);
  133. }
  134. function overrideCanvaRendProto(root) {
  135. const name = "getImageData";
  136. const getImageData = root.prototype.getImageData;
  137.  
  138. root.prototype[storedObjectPrefix + name] = getImageData;
  139.  
  140. Object.defineProperty(root.prototype, "getImageData",
  141. {
  142. value: function () {
  143. var imageData = getImageData.apply(this, arguments);
  144. var height = imageData.height;
  145. var width = imageData.width;
  146. // console.log("getImageData " + width + " " + height);
  147. for (var i = 0; i < height; i++) {
  148. for (var j = 0; j < width; j++) {
  149. var index = ((i * (width * 4)) + (j * 4));
  150. imageData.data[index + 0] = imageData.data[index + 0] + r;
  151. imageData.data[index + 1] = imageData.data[index + 1] + g;
  152. imageData.data[index + 2] = imageData.data[index + 2] + b;
  153. imageData.data[index + 3] = imageData.data[index + 3] + a;
  154. }
  155. }
  156. showNotification();
  157. return imageData;
  158. }
  159. }
  160. );
  161. }
  162. function inject(element) {
  163. if (element.tagName.toUpperCase() === "IFRAME" && element.contentWindow) {
  164. try {
  165. var hasAccess = element.contentWindow.HTMLCanvasElement;
  166. } catch (e) {
  167. console.log("can't access " + e);
  168. return;
  169. }
  170. overrideCanvasProto(element.contentWindow.HTMLCanvasElement);
  171. overrideCanvaRendProto(element.contentWindow.CanvasRenderingContext2D);
  172. overrideDocumentProto(element.contentWindow.Document);
  173. }
  174. }
  175. function overrideDocumentProto(root) {
  176. function doOverrideDocumentProto(old, name) {
  177. root.prototype[storedObjectPrefix + name] = old;
  178. Object.defineProperty(root.prototype, name,
  179. {
  180. value: function () {
  181. var element = old.apply(this, arguments);
  182. // console.log(name+ " everridden call"+element);
  183. if (element == null) {
  184. return null;
  185. }
  186. if (Object.prototype.toString.call(element) === '[object HTMLCollection]' ||
  187. Object.prototype.toString.call(element) === '[object NodeList]') {
  188. for (var i = 0; i < element.length; ++i) {
  189. var el = element[i];
  190. // console.log("elements list inject " + name);
  191. inject(el);
  192. }
  193. } else {
  194. // console.log("element inject " + name);
  195. inject(element);
  196. }
  197. return element;
  198. }
  199. }
  200. );
  201. }
  202. doOverrideDocumentProto(root.prototype.createElement, "createElement");
  203. doOverrideDocumentProto(root.prototype.createElementNS, "createElementNS");
  204. doOverrideDocumentProto(root.prototype.getElementById, "getElementById");
  205. doOverrideDocumentProto(root.prototype.getElementsByName, "getElementsByName");
  206. doOverrideDocumentProto(root.prototype.getElementsByClassName, "getElementsByClassName");
  207. doOverrideDocumentProto(root.prototype.getElementsByTagName, "getElementsByTagName");
  208. doOverrideDocumentProto(root.prototype.getElementsByTagNameNS, "getElementsByTagNameNS");
  209. }
  210. overrideCanvasProto(HTMLCanvasElement);
  211. overrideCanvaRendProto(CanvasRenderingContext2D);
  212. overrideDocumentProto(Document);
  213. scriptNode.parentNode.removeChild(scriptNode);
  214. }
  215.  
  216.  
  217. function restoreDefaultMethods(r, g, b, a, scriptId, storedObjectPrefix) {
  218. var scriptNode = document.getElementById(scriptId);
  219. function overrideCanvasProto(root) {
  220. function overrideCanvasInternal(name, old) {
  221. root.prototype[name] = root.prototype[storedObjectPrefix + name];
  222. }
  223. overrideCanvasInternal("toDataURL", root.prototype.toDataURL);
  224. overrideCanvasInternal("toBlob", root.prototype.toBlob);
  225. //overrideCanvasInternal("mozGetAsFile", root.prototype.mozGetAsFile);
  226. }
  227. function overrideCanvaRendProto(root) {
  228. const name = "getImageData";
  229. root.prototype[name] = root.prototype[storedObjectPrefix + name];
  230. }
  231. function inject(element) {
  232. if (element.tagName.toUpperCase() === "IFRAME" && element.contentWindow) {
  233. try {
  234. var hasAccess = element.contentWindow.HTMLCanvasElement;
  235. } catch (e) {
  236. console.log("can't access " + e);
  237. return;
  238. }
  239. overrideCanvasProto(element.contentWindow.HTMLCanvasElement);
  240. overrideCanvaRendProto(element.contentWindow.CanvasRenderingContext2D);
  241. overrideDocumentProto(element.contentWindow.Document);
  242. }
  243. }
  244. function overrideDocumentProto(root) {
  245. function doOverrideDocumentProto(old, name) {
  246. root.prototype[name] = root.prototype[storedObjectPrefix + name];
  247. }
  248. doOverrideDocumentProto(root.prototype.createElement, "createElement");
  249. doOverrideDocumentProto(root.prototype.createElementNS, "createElementNS");
  250. doOverrideDocumentProto(root.prototype.getElementById, "getElementById");
  251. doOverrideDocumentProto(root.prototype.getElementsByName, "getElementsByName");
  252. doOverrideDocumentProto(root.prototype.getElementsByClassName, "getElementsByClassName");
  253. doOverrideDocumentProto(root.prototype.getElementsByTagName, "getElementsByTagName");
  254. doOverrideDocumentProto(root.prototype.getElementsByTagNameNS, "getElementsByTagNameNS");
  255. }
  256. overrideCanvasProto(HTMLCanvasElement);
  257. overrideCanvaRendProto(CanvasRenderingContext2D);
  258. overrideDocumentProto(Document);
  259. scriptNode.parentNode.removeChild(scriptNode);
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement