app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. class App extends Events {
  2. _$ = $;
  3. _$$ = $$;
  4. _page = page;
  5. collections = {};
  6. models = {};
  7. templates = {};
  8. views = {};
  9. init() {
  10. try {
  11. this.initErrorTracking();
  12. } catch (error) {}
  13. if (!this.browserCheck()) {
  14. return;
  15. }
  16. this.el = $("._app");
  17. this.localStorage = new LocalStorageStore();
  18. if (app.ServiceWorker.isEnabled()) {
  19. this.serviceWorker = new app.ServiceWorker();
  20. }
  21. this.settings = new app.Settings();
  22. this.db = new app.DB();
  23. this.settings.initLayout();
  24. this.docs = new app.collections.Docs();
  25. this.disabledDocs = new app.collections.Docs();
  26. this.entries = new app.collections.Entries();
  27. this.router = new app.Router();
  28. this.shortcuts = new app.Shortcuts();
  29. this.document = new app.views.Document();
  30. if (this.isMobile()) {
  31. this.mobile = new app.views.Mobile();
  32. }
  33. if (document.body.hasAttribute("data-doc")) {
  34. this.DOC = JSON.parse(document.body.getAttribute("data-doc"));
  35. this.bootOne();
  36. } else if (this.DOCS) {
  37. this.bootAll();
  38. } else {
  39. this.onBootError();
  40. }
  41. }
  42. browserCheck() {
  43. if (this.isSupportedBrowser()) {
  44. return true;
  45. }
  46. document.body.innerHTML = app.templates.unsupportedBrowser;
  47. this.hideLoadingScreen();
  48. return false;
  49. }
  50. initErrorTracking() {
  51. // Show a warning message and don't track errors when the app is loaded
  52. // from a domain other than our own, because things are likely to break.
  53. // (e.g. cross-domain requests)
  54. if (this.isInvalidLocation()) {
  55. new app.views.Notif("InvalidLocation");
  56. } else {
  57. if (this.config.sentry_dsn) {
  58. Raven.config(this.config.sentry_dsn, {
  59. release: this.config.release,
  60. whitelistUrls: [/devdocs/],
  61. includePaths: [/devdocs/],
  62. ignoreErrors: [/NPObject/, /NS_ERROR/, /^null$/, /EvalError/],
  63. tags: {
  64. mode: this.isSingleDoc() ? "single" : "full",
  65. iframe: (window.top !== window).toString(),
  66. electron: (!!window.process?.versions?.electron).toString(),
  67. },
  68. shouldSendCallback: () => {
  69. try {
  70. if (this.isInjectionError()) {
  71. this.onInjectionError();
  72. return false;
  73. }
  74. if (this.isAndroidWebview()) {
  75. return false;
  76. }
  77. } catch (error) {}
  78. return true;
  79. },
  80. dataCallback(data) {
  81. try {
  82. $.extend(data.user || (data.user = {}), app.settings.dump());
  83. if (data.user.docs) {
  84. data.user.docs = data.user.docs.split("/");
  85. }
  86. if (app.lastIDBTransaction) {
  87. data.user.lastIDBTransaction = app.lastIDBTransaction;
  88. }
  89. data.tags.scriptCount = document.scripts.length;
  90. } catch (error) {}
  91. return data;
  92. },
  93. }).install();
  94. }
  95. this.previousErrorHandler = onerror;
  96. window.onerror = this.onWindowError.bind(this);
  97. CookiesStore.onBlocked = this.onCookieBlocked;
  98. }
  99. }
  100. bootOne() {
  101. this.doc = new app.models.Doc(this.DOC);
  102. this.docs.reset([this.doc]);
  103. this.doc.load(this.start.bind(this), this.onBootError.bind(this), {
  104. readCache: true,
  105. });
  106. new app.views.Notice("singleDoc", this.doc);
  107. delete this.DOC;
  108. }
  109. bootAll() {
  110. const docs = this.settings.getDocs();
  111. for (var doc of this.DOCS) {
  112. (docs.indexOf(doc.slug) >= 0 ? this.docs : this.disabledDocs).add(doc);
  113. }
  114. this.migrateDocs();
  115. this.docs.load(this.start.bind(this), this.onBootError.bind(this), {
  116. readCache: true,
  117. writeCache: true,
  118. });
  119. delete this.DOCS;
  120. }
  121. start() {
  122. let doc;
  123. for (doc of this.docs.all()) {
  124. this.entries.add(doc.toEntry());
  125. }
  126. for (doc of this.disabledDocs.all()) {
  127. this.entries.add(doc.toEntry());
  128. }
  129. for (doc of this.docs.all()) {
  130. this.initDoc(doc);
  131. }
  132. this.trigger("ready");
  133. this.router.start();
  134. this.hideLoadingScreen();
  135. setTimeout(() => {
  136. if (!this.doc) {
  137. this.welcomeBack();
  138. }
  139. return this.removeEvent("ready bootError");
  140. }, 50);
  141. }
  142. initDoc(doc) {
  143. for (var type of doc.types.all()) {
  144. doc.entries.add(type.toEntry());
  145. }
  146. this.entries.add(doc.entries.all());
  147. }
  148. migrateDocs() {
  149. let needsSaving;
  150. for (var slug of this.settings.getDocs()) {
  151. if (!this.docs.findBy("slug", slug)) {
  152. var doc;
  153. needsSaving = true;
  154. if (slug === "webpack~2") {
  155. doc = this.disabledDocs.findBy("slug", "webpack");
  156. }
  157. if (slug === "angular~4_typescript") {
  158. doc = this.disabledDocs.findBy("slug", "angular");
  159. }
  160. if (slug === "angular~2_typescript") {
  161. doc = this.disabledDocs.findBy("slug", "angular~2");
  162. }
  163. if (!doc) {
  164. doc = this.disabledDocs.findBy("slug_without_version", slug);
  165. }
  166. if (doc) {
  167. this.disabledDocs.remove(doc);
  168. this.docs.add(doc);
  169. }
  170. }
  171. }
  172. if (needsSaving) {
  173. this.saveDocs();
  174. }
  175. }
  176. enableDoc(doc, _onSuccess, onError) {
  177. if (this.docs.contains(doc)) {
  178. return;
  179. }
  180. const onSuccess = () => {
  181. if (this.docs.contains(doc)) {
  182. return;
  183. }
  184. this.disabledDocs.remove(doc);
  185. this.docs.add(doc);
  186. this.docs.sort();
  187. this.initDoc(doc);
  188. this.saveDocs();
  189. if (app.settings.get("autoInstall")) {
  190. doc.install(_onSuccess, onError);
  191. } else {
  192. _onSuccess();
  193. }
  194. };
  195. doc.load(onSuccess, onError, { writeCache: true });
  196. }
  197. saveDocs() {
  198. this.settings.setDocs(this.docs.all().map((doc) => doc.slug));
  199. this.db.migrate();
  200. return this.serviceWorker != null
  201. ? this.serviceWorker.updateInBackground()
  202. : undefined;
  203. }
  204. welcomeBack() {
  205. let visitCount = this.settings.get("count");
  206. this.settings.set("count", ++visitCount);
  207. if (visitCount === 5) {
  208. new app.views.Notif("Share", { autoHide: null });
  209. }
  210. new app.views.News();
  211. new app.views.Updates();
  212. return (this.updateChecker = new app.UpdateChecker());
  213. }
  214. reboot() {
  215. if (location.pathname !== "/" && location.pathname !== "/settings") {
  216. window.location = `/#${location.pathname}`;
  217. } else {
  218. window.location = "/";
  219. }
  220. }
  221. reload() {
  222. this.docs.clearCache();
  223. this.disabledDocs.clearCache();
  224. if (this.serviceWorker) {
  225. this.serviceWorker.reload();
  226. } else {
  227. this.reboot();
  228. }
  229. }
  230. reset() {
  231. this.localStorage.reset();
  232. this.settings.reset();
  233. if (this.db != null) {
  234. this.db.reset();
  235. }
  236. if (this.serviceWorker != null) {
  237. this.serviceWorker.update();
  238. }
  239. window.location = "/";
  240. }
  241. showTip(tip) {
  242. if (this.isSingleDoc()) {
  243. return;
  244. }
  245. const tips = this.settings.getTips();
  246. if (tips.indexOf(tip) === -1) {
  247. tips.push(tip);
  248. this.settings.setTips(tips);
  249. new app.views.Tip(tip);
  250. }
  251. }
  252. hideLoadingScreen() {
  253. if ($.overlayScrollbarsEnabled()) {
  254. document.body.classList.add("_overlay-scrollbars");
  255. }
  256. document.documentElement.classList.remove("_booting");
  257. }
  258. indexHost() {
  259. // Can't load the index files from the host/CDN when service worker is
  260. // enabled because it doesn't support caching URLs that use CORS.
  261. return this.config[
  262. this.serviceWorker && this.settings.hasDocs()
  263. ? "index_path"
  264. : "docs_origin"
  265. ];
  266. }
  267. onBootError(...args) {
  268. this.trigger("bootError");
  269. this.hideLoadingScreen();
  270. }
  271. onQuotaExceeded() {
  272. if (this.quotaExceeded) {
  273. return;
  274. }
  275. this.quotaExceeded = true;
  276. new app.views.Notif("QuotaExceeded", { autoHide: null });
  277. }
  278. onCookieBlocked(key, value, actual) {
  279. if (this.cookieBlocked) {
  280. return;
  281. }
  282. this.cookieBlocked = true;
  283. new app.views.Notif("CookieBlocked", { autoHide: null });
  284. Raven.captureMessage(`CookieBlocked/${key}`, {
  285. level: "warning",
  286. extra: { value, actual },
  287. });
  288. }
  289. onWindowError(...args) {
  290. if (this.cookieBlocked) {
  291. return;
  292. }
  293. if (this.isInjectionError(...args)) {
  294. this.onInjectionError();
  295. } else if (this.isAppError(...args)) {
  296. if (typeof this.previousErrorHandler === "function") {
  297. this.previousErrorHandler(...args);
  298. }
  299. this.hideLoadingScreen();
  300. if (!this.errorNotif) {
  301. this.errorNotif = new app.views.Notif("Error");
  302. }
  303. this.errorNotif.show();
  304. }
  305. }
  306. onInjectionError() {
  307. if (!this.injectionError) {
  308. this.injectionError = true;
  309. alert(`\
  310. JavaScript code has been injected in the page which prevents DevDocs from running correctly.
  311. Please check your browser extensions/addons. `);
  312. Raven.captureMessage("injection error", { level: "info" });
  313. }
  314. }
  315. isInjectionError() {
  316. // Some browser extensions expect the entire web to use jQuery.
  317. // I gave up trying to fight back.
  318. return (
  319. window.$ !== app._$ ||
  320. window.$$ !== app._$$ ||
  321. window.page !== app._page ||
  322. typeof $.empty !== "function" ||
  323. typeof page.show !== "function"
  324. );
  325. }
  326. isAppError(error, file) {
  327. // Ignore errors from external scripts.
  328. return (
  329. file &&
  330. file.indexOf("devdocs") !== -1 &&
  331. file.indexOf(".js") === file.length - 3
  332. );
  333. }
  334. isSupportedBrowser() {
  335. try {
  336. const features = {
  337. bind: !!Function.prototype.bind,
  338. pushState: !!history.pushState,
  339. matchMedia: !!window.matchMedia,
  340. insertAdjacentHTML: !!document.body.insertAdjacentHTML,
  341. defaultPrevented:
  342. document.createEvent("CustomEvent").defaultPrevented === false,
  343. cssVariables: !!CSS.supports?.("(--t: 0)"),
  344. };
  345. for (var key in features) {
  346. var value = features[key];
  347. if (!value) {
  348. Raven.captureMessage(`unsupported/${key}`, { level: "info" });
  349. return false;
  350. }
  351. }
  352. return true;
  353. } catch (error) {
  354. Raven.captureMessage("unsupported/exception", {
  355. level: "info",
  356. extra: { error },
  357. });
  358. return false;
  359. }
  360. }
  361. isSingleDoc() {
  362. return document.body.hasAttribute("data-doc");
  363. }
  364. isMobile() {
  365. return this._isMobile != null
  366. ? this._isMobile
  367. : (this._isMobile = app.views.Mobile.detect());
  368. }
  369. isAndroidWebview() {
  370. return this._isAndroidWebview != null
  371. ? this._isAndroidWebview
  372. : (this._isAndroidWebview = app.views.Mobile.detectAndroidWebview());
  373. }
  374. isInvalidLocation() {
  375. return (
  376. this.config.env === "production" &&
  377. location.host.indexOf(app.config.production_host) !== 0
  378. );
  379. }
  380. }
  381. this.app = new App();