This code appears to be a JavaScript snippet that handles events and requests when certain elements are clicked, and also fetches query parameters from the URL. However, it contains unused variables, potential mistakes in event listeners, and debugging statements that can be removed in production code.
npm run import -- "discord client code"
let code
let userId
function sourceWindow() {
return (window.parent.opener || window.parent)
}
document.addEventListener('click', async (evt) => {
if(evt.target.tagName == 'A' && evt.target.getAttribute('href').includes('://')) {
debugger
sourceWindow().postMessage([1, {
cmd: 'OPEN_EXTERNAL_LINK',
args: {url: evt.target.href},
nonce: Math.random() * 1000,
transfer: void 0
}], '*')
evt.preventDefault()
return false
}
if(evt.target.tagName == 'A' && evt.target.getAttribute('href').includes('/.proxy/')) {
const loggedInResponse = await fetch(evt.target.getAttribute('href'))
const newContent = await loggedInResponse.text()
//if(!newContent.includes('Cannot GET'))
document.body.innerHTML = newContent.replace(/<\/?html>/gi, '')
.replace(/<script.*?>[\s\S]*?<\/script>/gi, '');
}
return true
})
let result = {}
let params = (window.location.search.split('?')[1] || '').split('&')
for(var param in params) {
if (params.hasOwnProperty(param)) {
let paramParts = params[param].split('=')
result[paramParts[0]] = decodeURIComponent(paramParts[1] || "")
}
}
document.addEventListener("DOMContentLoaded", (event) => {
debugger
if(typeof result.frame_id == 'undefined') {
return
}
})
// Constants
const OPEN_EXTERNAL_LINK_CMD = 'OPEN_EXTERNAL_LINK';
const LOGIN_PROXY_URL_PARAM = '.proxy/';
// Utilities
function getSourceWindow() {
return (window.parent.opener || window.parent);
}
function getParameterByName(name, defaultValue = '') {
const param = (window.location.search.split('?')[1] || '').split('&').find(param => param.startsWith(`${name}=`));
return param? decodeURIComponent(param.split('=')[1]) : defaultValue;
}
function extractContent(html) {
return html.replace(/<\/?html>/gi, '').replace(/[\s\S]*?<\/script>/gi, '');
}
// Event listeners
document.addEventListener('click', async (evt) => {
try {
// Prevent default link behavior
if (evt.target.tagName === 'A' && evt.target.getAttribute('href').includes('://')) {
await openExternalLink(evt.target);
return;
}
// Load proxied content
if (evt.target.tagName === 'A' && evt.target.getAttribute('href').includes(LOGIN_PROXY_URL_PARAM)) {
const loggedInResponse = await fetch(evt.target.getAttribute('href'));
const newContent = await loggedInResponse.text();
await updateContent(extractContent(newContent));
}
} catch (error) {
console.error('Error handling click event:', error);
}
});
document.addEventListener("DOMContentLoaded", (event) => {
try {
// Get frame ID parameter
const frameId = getParameterByName('frame_id');
if (!frameId) {
return;
}
// Add functionality to handle frame ID here
console.log('Frame ID:', frameId);
} catch (error) {
console.error('Error handling DOM content loaded event:', error);
}
});
async function openExternalLink(link) {
const sourceWindow = getSourceWindow();
if (sourceWindow) {
const nonce = Math.random() * 1000;
sourceWindow.postMessage([1, {
cmd: OPEN_EXTERNAL_LINK_CMD,
args: { url: link.href },
nonce,
transfer: void 0,
}], '*');
}
link.preventDefault();
}
async function updateContent(content) {
document.body.innerHTML = content;
}
Code Breakdown
code
and userId
are declared but not used anywhere in the code.result
is an object that stores query parameters from the URL.sourceWindow()
returns the opener or parent window of the current window.When an element with a tag name of 'A' and a valid 'href' attribute is clicked:
When an element with a tag name of 'A' and a 'href' attribute containing '/.proxy/' is clicked:
result
object.result
object.debugger
statements, which are likely used for debugging purposes and can be removed in production code.