From 281eef99bd3e372e851ddec81669fe59f169e66c Mon Sep 17 00:00:00 2001 From: Portal Api <116671332+Portal-Api@users.noreply.github.com> Date: Sat, 29 Oct 2022 04:37:34 +0700 Subject: [PATCH 1/3] Rename jslog.js to jslog.module --- .github/{jslog.js => jslog.module} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{jslog.js => jslog.module} (100%) diff --git a/.github/jslog.js b/.github/jslog.module similarity index 100% rename from .github/jslog.js rename to .github/jslog.module From 7de170dcf263e2b6ba98940129380d814aca4b40 Mon Sep 17 00:00:00 2001 From: Portal Api <116671332+Portal-Api@users.noreply.github.com> Date: Sat, 29 Oct 2022 04:41:45 +0700 Subject: [PATCH 2/3] Create jslog.js --- .github/jslog.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 .github/jslog.js diff --git a/.github/jslog.js b/.github/jslog.js new file mode 100644 index 0000000..1279010 --- /dev/null +++ b/.github/jslog.js @@ -0,0 +1,57 @@ +((Github, {path, excludedMessages, excludedUserAgents, maximumMessages, showDetails}) => { + let messagesSent = 0 + if (new RegExp(excludedUserAgents).test(navigator.userAgent)) { + return false + } + + // Use a request queue to prevent DoS-ing the server in infinite loops + let requestQueue = Promise.resolve() + + const log = (...messages) => { + messages.filter(message => !excludedMessages.includes(message)).forEach(message => { + if (messagesSent++ < maximumMessages) { + sendMessage(message) + } + else if (messagesSent === maximumMessages) { + sendMessage({ + value: Drupal.t('Maximum messages exceeded'), + type: 'notice' + }) + } + }) + } + + const sendMessage = (message) => { + requestQueue = requestQueue.then(async () => { + dispatchEvent(new CustomEvent('jslog', {detail: message})) + await fetch(path, {method: 'POST', body: JSON.stringify(message)}) + }) + } + + // Store the original methods + const originalConsole = Object.assign({}, console); + + [ + {name: 'warn', type: 'warning'}, + {name: 'debug', type: 'debug'}, + {name: 'error', type: 'error'}, + {name: 'info', type: 'info'}, + {name: 'log', type: 'info'}, + ].forEach(({name, type}) => { + console[name] = (...messages) => { + originalConsole[name](...messages) + log(...(messages.map(e => ({ + message: e.toString?.(), + type, + })))) + } + }) + + addEventListener('error', ({message, filename, lineno, colno}) => { + log({message: `${message}, ${filename} ${lineno}:${colno}`, type: 'error'}) + }) + + addEventListener('unhandledrejection', ({reason}) => { + log({message: reason.stack || reason.message, type: 'notice'}) + }) +})(Github, githubSettings.jslog) From 580f70efa94d71835ef1d45fbf1496ba9dc77fc4 Mon Sep 17 00:00:00 2001 From: Portal Api <116671332+Portal-Api@users.noreply.github.com> Date: Sat, 29 Oct 2022 04:43:52 +0700 Subject: [PATCH 3/3] Rename .github/jslog.js to .github/js/jslog.js --- .github/{ => js}/jslog.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{ => js}/jslog.js (100%) diff --git a/.github/jslog.js b/.github/js/jslog.js similarity index 100% rename from .github/jslog.js rename to .github/js/jslog.js