chrome-extension/public/content-script.js

291 lines
9.7 KiB
JavaScript
Raw Normal View History

2024-09-09 20:36:39 +03:00
2024-04-14 14:57:30 +03:00
async function connection(hostname) {
const isConnected = await chrome.storage.local.get([hostname]);
let connected = false
if(isConnected && Object.keys(isConnected).length > 0 && isConnected[hostname]){
connected = true
}
return connected
}
// In your content script
document.addEventListener('qortalExtensionRequests', async (event) => {
const { type, payload, requestId, timeout } = event.detail; // Capture the requestId
if (type === 'REQUEST_USER_INFO') {
const hostname = window.location.hostname
const res = await connection(hostname)
2024-04-15 22:30:49 +03:00
2024-04-14 14:57:30 +03:00
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "userInfo" }, (response) => {
2024-04-14 14:57:30 +03:00
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: response, requestId }
}));
}
});
} else if (type === 'REQUEST_IS_INSTALLED') {
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "version" }, (response) => {
2024-04-14 14:57:30 +03:00
if (response.error) {
console.error("Error:", response.error);
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "IS_INSTALLED", data: response, requestId }
}));
}
});
} else if (type === 'REQUEST_CONNECTION') {
const hostname = window.location.hostname
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "connection", payload: {
2024-04-14 14:57:30 +03:00
hostname
}, timeout }, (response) => {
if (response.error) {
console.error("Error:", response.error);
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "CONNECTION", data: response, requestId }
2024-07-04 20:14:32 +03:00
}));
}
});
} else if (type === 'REQUEST_OAUTH') {
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "OAUTH", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-07-06 09:06:17 +03:00
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "oauth", payload: {
2024-07-06 09:06:17 +03:00
nodeBaseUrl: payload.nodeBaseUrl,
senderAddress: payload.senderAddress,
senderPublicKey: payload.senderPublicKey, timestamp: payload.timestamp
2024-07-04 20:14:32 +03:00
}}, (response) => {
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "OAUTH", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "OAUTH", data: response, requestId }
2024-04-14 14:57:30 +03:00
}));
}
});
2024-07-12 23:44:21 -04:00
} else if (type === 'REQUEST_BUY_ORDER') {
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "BUY_ORDER", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "buyOrder", payload: {
2024-09-18 00:08:19 +03:00
qortalAtAddresses: payload.qortalAtAddresses,
hostname,
useLocal: payload?.useLocal
2024-07-12 23:44:21 -04:00
}, timeout}, (response) => {
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "BUY_ORDER", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "BUY_ORDER", data: response, requestId }
}));
}
});
2024-07-17 17:00:43 -04:00
} else if(type === 'REQUEST_LTC_BALANCE'){
2024-09-09 20:36:39 +03:00
2024-07-17 17:00:43 -04:00
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "ltcBalance", payload: {
2024-07-17 17:00:43 -04:00
hostname
}, timeout }, (response) => {
2024-09-09 20:36:39 +03:00
2024-07-17 17:00:43 -04:00
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "LTC_BALANCE", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "LTC_BALANCE", data: response, requestId }
}));
}
});
2024-09-18 00:08:19 +03:00
} else if(type === 'CHECK_IF_LOCAL'){
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: "Not authorized"
}, requestId }
}));
return
}
chrome?.runtime?.sendMessage({ action: "checkLocal", payload: {
hostname
}, timeout }, (response) => {
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "CHECK_IF_LOCAL", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "CHECK_IF_LOCAL", data: response, requestId }
}));
}
});
2024-07-12 23:44:21 -04:00
} else if (type === 'REQUEST_AUTHENTICATION') {
2024-04-14 14:57:30 +03:00
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "authentication", payload: {
2024-04-14 14:57:30 +03:00
hostname
}, timeout }, (response) => {
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "AUTHENTICATION", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "AUTHENTICATION", data: response, requestId }
}));
}
});
} else if (type === 'REQUEST_SEND_QORT') {
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "sendQort", payload: {
2024-04-14 14:57:30 +03:00
hostname,
amount: payload.amount,
description: payload.description,
address: payload.address
}, timeout }, (response) => {
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "SEND_QORT", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "SEND_QORT", data: response, requestId }
}));
}
});
} else if (type === 'REQUEST_CLOSE_POPUP') {
const hostname = window.location.hostname
const res = await connection(hostname)
if(!res){
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "USER_INFO", data: {
error: "Not authorized"
}, requestId }
}));
return
}
2024-09-12 05:01:36 +03:00
chrome?.runtime?.sendMessage({ action: "closePopup" }, (response) => {
2024-04-14 14:57:30 +03:00
if (response.error) {
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "CLOSE_POPUP", data: {
error: response.error
}, requestId }
}));
} else {
// Include the requestId in the detail when dispatching the response
document.dispatchEvent(new CustomEvent('qortalExtensionResponses', {
detail: { type: "CLOSE_POPUP", data: true, requestId }
}));
}
});
}
// Handle other request types as needed...
});
2024-07-09 06:58:45 +03:00
2024-09-12 05:01:36 +03:00
chrome.runtime?.onMessage.addListener(function(message, sender, sendResponse) {
2024-07-09 06:58:45 +03:00
if (message.type === "LOGOUT") {
// Notify the web page
window.postMessage({
type: "LOGOUT",
from: 'qortal'
}, "*");
2024-07-15 22:07:57 -04:00
} else if (message.type === "RESPONSE_FOR_TRADES") {
// Notify the web page
window.postMessage({
type: "RESPONSE_FOR_TRADES",
from: 'qortal',
payload: message.message
}, "*");
}
});