hackmd-agent.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. function j(r) {
  2. return r && r.__esModule && Object.prototype.hasOwnProperty.call(r, "default") ? r.default : r;
  3. }
  4. var F = { exports: {} }, i = {};
  5. Object.defineProperty(i, "__esModule", {
  6. value: !0
  7. });
  8. i.DATA_CLONE_ERROR = i.MESSAGE = i.REJECTED = i.FULFILLED = i.REPLY = i.CALL = i.HANDSHAKE_REPLY = i.HANDSHAKE = void 0;
  9. const J = "handshake";
  10. i.HANDSHAKE = J;
  11. const W = "handshake-reply";
  12. i.HANDSHAKE_REPLY = W;
  13. const B = "call";
  14. i.CALL = B;
  15. const q = "reply";
  16. i.REPLY = q;
  17. const k = "fulfilled";
  18. i.FULFILLED = k;
  19. const Q = "rejected";
  20. i.REJECTED = Q;
  21. const X = "message";
  22. i.MESSAGE = X;
  23. const Z = "DataCloneError";
  24. i.DATA_CLONE_ERROR = Z;
  25. var N = {};
  26. Object.defineProperty(N, "__esModule", {
  27. value: !0
  28. });
  29. N.ERR_NO_IFRAME_SRC = N.ERR_NOT_IN_IFRAME = N.ERR_CONNECTION_TIMEOUT = N.ERR_CONNECTION_DESTROYED = void 0;
  30. const ee = "ConnectionDestroyed";
  31. N.ERR_CONNECTION_DESTROYED = ee;
  32. const re = "ConnectionTimeout";
  33. N.ERR_CONNECTION_TIMEOUT = re;
  34. const te = "NotInIframe";
  35. N.ERR_NOT_IN_IFRAME = te;
  36. const ne = "NoIframeSrc";
  37. N.ERR_NO_IFRAME_SRC = ne;
  38. var $ = { exports: {} };
  39. (function(r, e) {
  40. Object.defineProperty(e, "__esModule", {
  41. value: !0
  42. }), e.default = void 0;
  43. var t = () => {
  44. const n = [];
  45. let c = !1;
  46. return {
  47. destroy() {
  48. c = !0, n.forEach((a) => {
  49. a();
  50. });
  51. },
  52. onDestroy(a) {
  53. c ? a() : n.push(a);
  54. }
  55. };
  56. };
  57. e.default = t, r.exports = e.default;
  58. })($, $.exports);
  59. var oe = $.exports, H = { exports: {} }, A = {};
  60. Object.defineProperty(A, "__esModule", {
  61. value: !0
  62. });
  63. A.deserializeError = A.serializeError = void 0;
  64. const ae = (r) => {
  65. let e = r.name, t = r.message, n = r.stack;
  66. return {
  67. name: e,
  68. message: t,
  69. stack: n
  70. };
  71. };
  72. A.serializeError = ae;
  73. const ie = (r) => {
  74. const e = new Error();
  75. return Object.keys(r).forEach((t) => e[t] = r[t]), e;
  76. };
  77. A.deserializeError = ie;
  78. (function(r, e) {
  79. Object.defineProperty(e, "__esModule", {
  80. value: !0
  81. }), e.default = void 0;
  82. var t = i, n = A, c = (a, u, v) => {
  83. const s = a.localName, _ = a.local, E = a.remote, p = a.originForSending, l = a.originForReceiving;
  84. let f = !1;
  85. v(`${s}: Connecting call receiver`);
  86. const O = (d) => {
  87. if (d.source !== E || d.data.penpal !== t.CALL)
  88. return;
  89. if (d.origin !== l) {
  90. v(`${s} received message from origin ${d.origin} which did not match expected origin ${l}`);
  91. return;
  92. }
  93. const h = d.data, D = h.methodName, S = h.args, T = h.id;
  94. v(`${s}: Received ${D}() call`);
  95. const g = (R) => (m) => {
  96. if (v(`${s}: Sending ${D}() reply`), f) {
  97. v(`${s}: Unable to send ${D}() reply due to destroyed connection`);
  98. return;
  99. }
  100. const C = {
  101. penpal: t.REPLY,
  102. id: T,
  103. resolution: R,
  104. returnValue: m
  105. };
  106. R === t.REJECTED && m instanceof Error && (C.returnValue = (0, n.serializeError)(m), C.returnValueIsError = !0);
  107. try {
  108. E.postMessage(C, p);
  109. } catch (L) {
  110. throw L.name === t.DATA_CLONE_ERROR && E.postMessage({
  111. penpal: t.REPLY,
  112. id: T,
  113. resolution: t.REJECTED,
  114. returnValue: (0, n.serializeError)(L),
  115. returnValueIsError: !0
  116. }, p), L;
  117. }
  118. };
  119. new Promise((R) => R(u[D].apply(u, S))).then(g(t.FULFILLED), g(t.REJECTED));
  120. };
  121. return _.addEventListener(t.MESSAGE, O), () => {
  122. f = !0, _.removeEventListener(t.MESSAGE, O);
  123. };
  124. };
  125. e.default = c, r.exports = e.default;
  126. })(H, H.exports);
  127. var se = H.exports, x = { exports: {} }, Y = { exports: {} };
  128. (function(r, e) {
  129. Object.defineProperty(e, "__esModule", {
  130. value: !0
  131. }), e.default = void 0;
  132. let t = 0;
  133. var n = () => ++t;
  134. e.default = n, r.exports = e.default;
  135. })(Y, Y.exports);
  136. var ce = Y.exports;
  137. (function(r, e) {
  138. Object.defineProperty(e, "__esModule", {
  139. value: !0
  140. }), e.default = void 0;
  141. var t = i, n = N, c = u(ce), a = A;
  142. function u(s) {
  143. return s && s.__esModule ? s : { default: s };
  144. }
  145. var v = (s, _, E, p, l) => {
  146. const f = _.localName, O = _.local, d = _.remote, h = _.originForSending, D = _.originForReceiving;
  147. let S = !1;
  148. l(`${f}: Connecting call sender`);
  149. const T = (g) => function() {
  150. for (var R = arguments.length, m = new Array(R), C = 0; C < R; C++)
  151. m[C] = arguments[C];
  152. l(`${f}: Sending ${g}() call`);
  153. let L;
  154. try {
  155. d.closed && (L = !0);
  156. } catch {
  157. L = !0;
  158. }
  159. if (L && p(), S) {
  160. const w = new Error(`Unable to send ${g}() call due to destroyed connection`);
  161. throw w.code = n.ERR_CONNECTION_DESTROYED, w;
  162. }
  163. return new Promise((w, P) => {
  164. const M = (0, c.default)(), I = (o) => {
  165. if (o.source !== d || o.data.penpal !== t.REPLY || o.data.id !== M)
  166. return;
  167. if (o.origin !== D) {
  168. l(`${f} received message from origin ${o.origin} which did not match expected origin ${D}`);
  169. return;
  170. }
  171. l(`${f}: Received ${g}() reply`), O.removeEventListener(t.MESSAGE, I);
  172. let y = o.data.returnValue;
  173. o.data.returnValueIsError && (y = (0, a.deserializeError)(y)), (o.data.resolution === t.FULFILLED ? w : P)(y);
  174. };
  175. O.addEventListener(t.MESSAGE, I), d.postMessage({
  176. penpal: t.CALL,
  177. id: M,
  178. methodName: g,
  179. args: m
  180. }, h);
  181. });
  182. };
  183. return E.reduce((g, R) => (g[R] = T(R), g), s), () => {
  184. S = !0;
  185. };
  186. };
  187. e.default = v, r.exports = e.default;
  188. })(x, x.exports);
  189. var de = x.exports, G = { exports: {} };
  190. (function(r, e) {
  191. Object.defineProperty(e, "__esModule", {
  192. value: !0
  193. }), e.default = void 0;
  194. var t = (n) => function() {
  195. if (n) {
  196. for (var c = arguments.length, a = new Array(c), u = 0; u < c; u++)
  197. a[u] = arguments[u];
  198. console.log("[Penpal]", ...a);
  199. }
  200. };
  201. e.default = t, r.exports = e.default;
  202. })(G, G.exports);
  203. var le = G.exports;
  204. (function(r, e) {
  205. Object.defineProperty(e, "__esModule", {
  206. value: !0
  207. }), e.default = void 0;
  208. var t = i, n = N, c = s(oe), a = s(se), u = s(de), v = s(le);
  209. function s(E) {
  210. return E && E.__esModule ? E : { default: E };
  211. }
  212. var _ = function() {
  213. let p = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, l = p.parentOrigin, f = l === void 0 ? "*" : l, O = p.methods, d = O === void 0 ? {} : O, h = p.timeout, D = p.debug;
  214. const S = (0, v.default)(D);
  215. if (window === window.top) {
  216. const w = new Error("connectToParent() must be called within an iframe");
  217. throw w.code = n.ERR_NOT_IN_IFRAME, w;
  218. }
  219. const T = (0, c.default)(), g = T.destroy, R = T.onDestroy, m = window, C = m.parent;
  220. return {
  221. promise: new Promise((w, P) => {
  222. let M;
  223. h !== void 0 && (M = setTimeout(() => {
  224. const o = new Error(`Connection to parent timed out after ${h}ms`);
  225. o.code = n.ERR_CONNECTION_TIMEOUT, P(o), g();
  226. }, h));
  227. const I = (o) => {
  228. try {
  229. clearTimeout();
  230. } catch {
  231. return;
  232. }
  233. if (o.source !== C || o.data.penpal !== t.HANDSHAKE_REPLY)
  234. return;
  235. if (f !== "*" && f !== o.origin) {
  236. S(`Child received handshake reply from origin ${o.origin} which did not match expected origin ${f}`);
  237. return;
  238. }
  239. S("Child: Received handshake reply"), m.removeEventListener(t.MESSAGE, I);
  240. const y = {
  241. localName: "Child",
  242. local: m,
  243. remote: C,
  244. originForSending: o.origin === "null" ? "*" : o.origin,
  245. originForReceiving: o.origin
  246. }, z = {}, K = (0, a.default)(y, d, S);
  247. R(K);
  248. const b = (0, u.default)(z, y, o.data.methodNames, g, S);
  249. R(b), clearTimeout(M), w(z);
  250. };
  251. m.addEventListener(t.MESSAGE, I), R(() => {
  252. m.removeEventListener(t.MESSAGE, I);
  253. const o = new Error("Connection destroyed");
  254. o.code = n.ERR_CONNECTION_DESTROYED, P(o);
  255. }), S("Child: Sending handshake"), C.postMessage({
  256. penpal: t.HANDSHAKE,
  257. methodNames: Object.keys(d)
  258. }, f);
  259. }),
  260. destroy: g
  261. };
  262. };
  263. e.default = _, r.exports = e.default;
  264. })(F, F.exports);
  265. var ue = F.exports;
  266. const Ee = /* @__PURE__ */ j(ue);
  267. function U(r, e, t, n) {
  268. var c, a = !1, u = 0;
  269. function v() {
  270. c && clearTimeout(c);
  271. }
  272. function s() {
  273. v(), a = !0;
  274. }
  275. typeof e != "boolean" && (n = t, t = e, e = void 0);
  276. function _() {
  277. for (var E = arguments.length, p = new Array(E), l = 0; l < E; l++)
  278. p[l] = arguments[l];
  279. var f = this, O = Date.now() - u;
  280. if (a)
  281. return;
  282. function d() {
  283. u = Date.now(), t.apply(f, p);
  284. }
  285. function h() {
  286. c = void 0;
  287. }
  288. n && !c && d(), v(), n === void 0 && O > r ? d() : e !== !0 && (c = setTimeout(n ? h : d, n === void 0 ? r - O : r));
  289. }
  290. return _.cancel = s, _;
  291. }
  292. function fe(r, e, t) {
  293. return t === void 0 ? U(r, e, !1) : U(r, t, e !== !1);
  294. }
  295. const ge = !1, Re = "<%= origin %>";
  296. function _e() {
  297. return window.editor.doc.getValue();
  298. }
  299. function V(r) {
  300. window.editor.doc.setValue(r);
  301. }
  302. function ve(r) {
  303. if (window.cmClient != null) {
  304. V(r);
  305. return;
  306. }
  307. const e = setInterval(() => {
  308. window.cmClient != null && (clearInterval(e), V(r));
  309. }, 250);
  310. }
  311. function pe(r) {
  312. window.growi.notifyBodyChanges(r);
  313. }
  314. const me = fe(800, pe);
  315. function Ce(r) {
  316. window.growi.saveWithShortcut(r);
  317. }
  318. function Oe() {
  319. const r = window.CodeMirror, e = window.editor;
  320. r == null || e == null || (e.on("change", (t, n) => {
  321. n.origin !== "ignoreHistory" && me(t.doc.getValue());
  322. }), r.commands.save = function(t) {
  323. Ce(t.doc.getValue());
  324. }, delete e.options.extraKeys["Cmd-S"], delete e.options.extraKeys["Ctrl-S"]);
  325. }
  326. function he() {
  327. Ee({
  328. parentOrigin: Re,
  329. // Methods child is exposing to parent
  330. methods: {
  331. getValue() {
  332. return _e();
  333. },
  334. setValue(e) {
  335. V(e);
  336. },
  337. setValueOnInit(e) {
  338. ve(e);
  339. }
  340. },
  341. debug: ge
  342. }).promise.then((e) => {
  343. window.growi = e;
  344. }).catch((e) => {
  345. console.log(e);
  346. });
  347. }
  348. (function() {
  349. if (window === window.parent) {
  350. console.log("[GROWI] Loading agent for HackMD is not processed because currently not in iframe");
  351. return;
  352. }
  353. console.log("[HackMD] Loading GROWI agent for HackMD..."), window.addEventListener("load", () => {
  354. Oe();
  355. }), he(), console.log("[HackMD] GROWI agent for HackMD has successfully loaded.");
  356. })();
  357. //# sourceMappingURL=hackmd-agent.js.map