Guest User

Untitled

a guest
May 3rd, 2018
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.26 KB | None | 0 0
  1. (function() {
  2. /**
  3. * Check and set a global guard variable.
  4. * If this content script is injected into the same page again,
  5. * it will do nothing next time.
  6. */
  7. if (window.hasRun) {
  8. return;
  9. }
  10. window.hasRun = true;
  11.  
  12. /**
  13. * Listen for messages from the background script.
  14. * Call the "Summarizer" function
  15. */
  16. browser.runtime.onMessage.addListener((message) => {
  17.  
  18. const outerDivID = "rstudios-web-extension-summarizer-88A178C0FD69FEDDB3ACFD0F5234D949AB2EE6543718774D5E2D331BF8376B99-fruit-jam-4EEC889A8A3E73A7BF00773FDD16AC859E7AEF517673DFD7C143BC327CBBC30C";
  19.  
  20. if (!document.getElementById(outerDivID)) {
  21.  
  22. const cleanslateCSS_ID = "rstudios-web-extension-summarizer-cleanslate-CSS";
  23.  
  24. if (!document.getElementById(cleanslateCSS_ID)) {
  25. var head = document.getElementsByTagName("head")[0];
  26. var link = document.createElement("link");
  27. link.id = cleanslateCSS_ID;
  28. link.rel = "stylesheet";
  29. link.type = "text/css";
  30. link.href = browser.extension.getURL("inject-content/cleanslate.css");
  31. head.appendChild(link);
  32. }
  33.  
  34. var iFrame = document.createElement("iFrame");
  35. iFrame.id = "contentFrame";
  36. iFrame.classList.add("cleanslate");
  37. iFrame.style.cssText = "width: 100% !important; height: 100% !important; border: none !important;";
  38. iFrame.src = browser.extension.getURL("inject-content/inject.html");
  39.  
  40. var boxDiv = document.createElement("div");
  41. boxDiv.classList.add("cleanslate");
  42. boxDiv.style.cssText = "background: white !important; box-shadow: rgba(0, 0, 0, 0.1) 0px 0px 9px 8px !important; height: 100% !important; left: calc(100% - 390px) !important; position: fixed !important; top: 0px !important; width: 390px !important; padding: 0px !important; z-index: 1 !important;"
  43.  
  44. var zeroDiv = document.createElement("div");
  45. zeroDiv.classList.add("cleanslate");
  46. zeroDiv.style.cssText = "position: fixed !important; width: 0px !important; height: 0px !important; top: 0px !important; left: 0px !important; z-index: 2147483647 !important;";
  47.  
  48. var outerDiv = document.createElement("div");
  49. zeroDiv.classList.add("cleanslate");
  50. outerDiv.id = outerDivID;
  51.  
  52. boxDiv.appendChild(iFrame);
  53. zeroDiv.appendChild(boxDiv);
  54. outerDiv.appendChild(zeroDiv);
  55. document.body.appendChild(outerDiv);
  56.  
  57. iFrame.onload = () => {
  58. var closeButton = iFrame.contentWindow.document.getElementById("close-btn");
  59.  
  60. closeButton.addEventListener("click", () => {
  61. outerDiv.remove();
  62. });
  63.  
  64. var copyButton = iFrame.contentWindow.document.getElementById("copy-btn");
  65.  
  66. copyButton.addEventListener("click", () => {
  67. let summaryContainer = iFrame.contentWindow.document.getElementById("summary");
  68. var textarea_temp = document.createElement("textarea");
  69. textarea_temp.style.position = 'fixed';
  70. textarea_temp.style.top = 0;
  71. textarea_temp.style.left = 0;
  72. textarea_temp.style.width = '2em';
  73. textarea_temp.style.height = '2em';
  74. textarea_temp.style.padding = 0;
  75. textarea_temp.style.border = 'none';
  76. textarea_temp.style.outline = 'none';
  77. textarea_temp.style.boxShadow = 'none';
  78. textarea_temp.style.background = 'transparent';
  79. textarea_temp.value = summaryContainer.innerHTML;
  80. document.body.appendChild(textarea_temp);
  81. textarea_temp.focus();
  82. textarea_temp.select();
  83. try {
  84. var successful = document.execCommand('copy');
  85. var msg = successful ? 'successful' : 'unsuccessful';
  86. console.log('Copying text command was ' + msg);
  87. } catch (error) {
  88. console.log('Error: ' + error);
  89. }
  90.  
  91. document.body.removeChild(textarea_temp);
  92.  
  93. });
  94.  
  95. updateSummaryBox(message.summaryLength, message.targetURL);
  96. }
  97. } else {
  98. updateSummaryBox(message.summaryLength, message.targetURL);
  99. }
  100. });
  101.  
  102. const updateSummaryBox = function(summaryLength, summaryURL) {
  103.  
  104. var summaryBox = document.getElementById("contentFrame").contentWindow.document.getElementById("summary");
  105. summaryBox.innerHTML = "Loading...";
  106. summaryBox.style.setProperty("color", "#52575C");
  107.  
  108.  
  109. const summary = returnSummary(summaryLength, summaryURL).then(summary => {
  110. if (summary.status == "no-error") {
  111. summaryBox.innerHTML = summary.summary;
  112. } else {
  113. summaryBox.innerHTML = summary.status;
  114. summaryBox.style.setProperty("color", "#9E0E28");
  115. }
  116. }, error => {
  117. if (error.message.includes("NetworkError")) {
  118. summaryBox.innerHTML = returnExceptionString(error, "returnSummary()", "This error can happen if your internet connection is blocking access to certain websites, including the one this extension uses to generate summaries.");
  119. } else {
  120. summaryBox.innerHTML = returnExceptionString(error, "returnSummary()");
  121. }
  122. summaryBox.style.setProperty("color", "#9E0E28");
  123. });
  124. }
  125.  
  126. const returnSummary = function(summaryLength, targetURL) {
  127.  
  128. const nonTokenURL = 'https://smmry.com/' + targetURL + '#&SM_LENGTH=' + summaryLength.toString();
  129.  
  130. return new Promise((resolve, reject) => {
  131. fetch(nonTokenURL)
  132. .then((nonTokenResponse) => nonTokenResponse.text())
  133. .then(nonTokenResponseText => {
  134. const summaryToken = nonTokenResponseText.match(/TOKEN=(.*?)&/);
  135. const summaryTokenCompiledURL = 'https://smmry.com/sm_portal.php?&SM_TOKEN=' + summaryToken[1] + '&SM_POST_SAVE=0&SM_REDUCTION=-1&SM_CHARACTER=-1&SM_LENGTH=' + summaryLength.toString() + '&SM_URL=' + targetURL;
  136. return fetch(summaryTokenCompiledURL);
  137. }).then((tokenSummaryResponse) => tokenSummaryResponse.text())
  138. .then(tokenResponseText_initial => {
  139. tokenResponseText_processed = tokenResponseText_initial.replace(/(\[SM_g].*?)(\[SM_h])((?:[.,?:!;%*+-<>=@_~^]|\[SM_l]| ?&quot;| ?&#039;| ?\\"){0,3})/g, "$1$3$2");
  140.  
  141. const wordCompilationRegex = /\[SM_g]([\s\S]*?)\[SM_h]/g;
  142. var wordRegexResponse;
  143.  
  144. var summary = "";
  145.  
  146. do {
  147. wordRegexResponse = wordCompilationRegex.exec(tokenResponseText_processed);
  148. if (wordRegexResponse) {
  149.  
  150. wordRegexResponse[1] = wordRegexResponse[1].replace("[SM_l]", "\n\n");
  151.  
  152. wordRegexResponse[1] = wordRegexResponse[1].replace('\\"', '"');
  153.  
  154. if (wordRegexResponse[1].includes(" &#039;") || wordRegexResponse[1].includes(" &quot;") || wordRegexResponse[1].includes(" \"")) {
  155. summary += wordRegexResponse[1];
  156. } else {
  157. summary += wordRegexResponse[1] + " ";
  158. }
  159. }
  160. } while (wordRegexResponse);
  161.  
  162. summary = summary.replace(/&#039;/g, '\'');
  163.  
  164. resolve(returnSummaryResponse(summary, tokenResponseText_initial));
  165. }).catch((error) => reject(error));
  166. });
  167. }
  168.  
  169. const returnSummaryResponse = function(summary, rawTokenText) {
  170. if (summary != "") {
  171. return { status: 'no-error', summary: summary };
  172. } else if (rawTokenText.includes('THE PAGE IS IN AN UNRECOGNISABLE FORMAT')) {
  173. return { status: 'Error - The Page Is in an Unrecognisable Format', summary: null }
  174. } else if (rawTokenText.includes('SOURCE IS TOO SHORT')) {
  175. return { status: 'Error - Source Is Too Short', summary: null };
  176. } else if (rawTokenText.includes('THE PAGE IS TOO LONG TO RETRIEVE')) {
  177. return { status: 'Error - The Page Is Too Long to Retrieve', summary: null };
  178. } else {
  179. return { status: 'Error - Unknown Error', summary: null };
  180. }
  181. }
  182.  
  183. const returnExceptionString = function(error, methodName, customMessage) {
  184. var exceptionString = "Handled Exception: " + error.message + "\nFile: " + error.fileName + "\nMethod: " + methodName + "\nLine: " + error.lineNumber + "\nMessage: ";
  185. if (customMessage != undefined) {
  186. return exceptionString + customMessage;
  187. } else {
  188. return exceptionString + "This is an unexpected error. Please report the above information to the developer.";
  189. }
  190. }
  191. })();
Advertisement
Add Comment
Please, Sign In to add comment