123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- export function automaFetchClient(id, { type, resource }) {
- return new Promise((resolve, reject) => {
- const validType = ['text', 'json', 'base64'];
- if (!type || !validType.includes(type)) {
- reject(new Error('The "type" must be "text" or "json"'));
- return;
- }
- const eventName = `__autom-fetch-response-${id}__`;
- const eventListener = ({ detail }) => {
- if (detail.id !== id) return;
- window.removeEventListener(eventName, eventListener);
- if (detail.isError) {
- reject(new Error(detail.result));
- } else {
- resolve(detail.result);
- }
- };
- window.addEventListener(eventName, eventListener);
- window.dispatchEvent(
- new CustomEvent(`__automa-fetch__`, {
- detail: {
- id,
- type,
- resource,
- },
- })
- );
- });
- }
- export function jsContentHandler($blockData, $preloadScripts, $automaScript) {
- return new Promise((resolve, reject) => {
- try {
- let $documentCtx = document;
- if ($blockData.frameSelector) {
- const iframeCtx = document.querySelector(
- $blockData.frameSelector
- )?.contentDocument;
- if (!iframeCtx) {
- reject(new Error('iframe-not-found'));
- return;
- }
- $documentCtx = iframeCtx;
- }
- const scriptAttr = `block--${$blockData.id}`;
- const isScriptExists = $documentCtx.querySelector(
- `.automa-custom-js[${scriptAttr}]`
- );
- if (isScriptExists) {
- resolve('');
- return;
- }
- const script = document.createElement('script');
- script.setAttribute(scriptAttr, '');
- script.classList.add('automa-custom-js');
- script.textContent = `(() => {
- ${$automaScript}
- try {
- ${$blockData.data.code}
- ${
- $blockData.data.everyNewTab ||
- $blockData.data.code.includes('automaNextBlock')
- ? ''
- : 'automaNextBlock()'
- }
- } catch (error) {
- console.error(error);
- ${
- $blockData.data.everyNewTab
- ? ''
- : 'automaNextBlock({ $error: true, message: error.message })'
- }
- }
- })()`;
- const preloadScriptsEl = $preloadScripts.map((item) => {
- const scriptEl = document.createElement('script');
- scriptEl.id = item.id;
- scriptEl.textContent = item.script;
- $documentCtx.head.appendChild(scriptEl);
- return { element: scriptEl, removeAfterExec: item.removeAfterExec };
- });
- if (!$blockData.data.everyNewTab) {
- let timeout;
- let onNextBlock;
- let onResetTimeout;
- /* eslint-disable-next-line */
- function cleanUp() {
- script.remove();
- preloadScriptsEl.forEach((item) => {
- if (item.removeAfterExec) item.script.remove();
- });
- clearTimeout(timeout);
- $documentCtx.body.removeEventListener(
- '__automa-reset-timeout__',
- onResetTimeout
- );
- $documentCtx.body.removeEventListener(
- '__automa-next-block__',
- onNextBlock
- );
- }
- onNextBlock = ({ detail }) => {
- cleanUp(detail || {});
- resolve({
- columns: {
- data: detail?.data,
- insert: detail?.insert,
- },
- variables: detail?.refData?.variables,
- });
- };
- onResetTimeout = () => {
- clearTimeout(timeout);
- timeout = setTimeout(cleanUp, $blockData.data.timeout);
- };
- $documentCtx.body.addEventListener(
- '__automa-next-block__',
- onNextBlock
- );
- $documentCtx.body.addEventListener(
- '__automa-reset-timeout__',
- onResetTimeout
- );
- timeout = setTimeout(cleanUp, $blockData.data.timeout);
- } else {
- resolve();
- }
- $documentCtx.head.appendChild(script);
- } catch (error) {
- console.error(error);
- }
- });
- }
|