add inpage api
This commit is contained in:
parent
11789fe1a1
commit
9b5284d457
5 changed files with 89 additions and 3 deletions
|
|
@ -22,6 +22,12 @@
|
|||
{
|
||||
"matches": ["<all_urls>"],
|
||||
"js": ["static/js/content.bundle.js"]
|
||||
},
|
||||
{
|
||||
"world": "MAIN",
|
||||
"js": ["static/js/inject.bundle.js"],
|
||||
"matches": ["<all_urls>"],
|
||||
"run_at": "document_start"
|
||||
}
|
||||
],
|
||||
"icons": {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ chrome.runtime.onStartup.addListener(() => {
|
|||
|
||||
// eslint-disable-next-line no-undef
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.message === "GET_WALLET_BALANCE") {
|
||||
if (request.method === "GET_WALLET_BALANCE") {
|
||||
fetchData("getbalance")
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
|
|
@ -24,7 +24,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|||
|
||||
// eslint-disable-next-line no-undef
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.message === "GET_WALLET_DATA") {
|
||||
if (request.method === "GET_WALLET_DATA") {
|
||||
console.log("Getting wallet data");
|
||||
getWalletData()
|
||||
.then((data) => {
|
||||
|
|
@ -40,7 +40,7 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
|||
|
||||
// eslint-disable-next-line no-undef
|
||||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
|
||||
if (request.message === "SEND_TRANSFER") {
|
||||
if (request.method === "SEND_TRANSFER") {
|
||||
console.log("Sending transfer");
|
||||
console.log("destination", request.destination, "amount", request.amount);
|
||||
transfer(request.destination, request.amount)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
async function fetchData(data) {
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
chrome.runtime.sendMessage(data, function (response) {
|
||||
resolve(response);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error while fetching data (${method}):`, error);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
document.addEventListener('zano_request', async (e) => {
|
||||
const data = e.detail;
|
||||
const response = await fetchData(data);
|
||||
|
||||
document.dispatchEvent(new CustomEvent(`zano_response_${data.listenerID}`, {
|
||||
detail: response
|
||||
}));
|
||||
|
||||
});
|
||||
|
||||
console.log('Zano wallet loaded');
|
||||
47
src/content/inject.js
Normal file
47
src/content/inject.js
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
class Zano {
|
||||
async request(method, params) {
|
||||
|
||||
function getRandonString(length) {
|
||||
let chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
let charLength = chars.length;
|
||||
let result = '';
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(Math.floor(Math.random() * charLength));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
const listenerID = getRandonString(16);
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
reject('Request timeout exceeded');
|
||||
document.removeEventListener(`zano_response_${listenerID}`, handleResponse);
|
||||
}, 30000);
|
||||
|
||||
function handleResponse(e) {
|
||||
document.removeEventListener(`zano_response_${listenerID}`, handleResponse);
|
||||
clearTimeout(timeout);
|
||||
resolve(e.detail);
|
||||
}
|
||||
|
||||
document.addEventListener(`zano_response_${listenerID}`, handleResponse);
|
||||
|
||||
document.dispatchEvent(new CustomEvent('zano_request', {
|
||||
detail: {
|
||||
method: method,
|
||||
listenerID: listenerID,
|
||||
...params
|
||||
}
|
||||
}));
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
window.zano = new Zano();
|
||||
|
|
@ -35,6 +35,14 @@ const contentConfig = {
|
|||
},
|
||||
};
|
||||
|
||||
const injectConfig = {
|
||||
entry: "./src/content/inject.js",
|
||||
output: {
|
||||
path: path.resolve(__dirname, "build/static/js"),
|
||||
filename: "inject.bundle.js",
|
||||
},
|
||||
};
|
||||
|
||||
const copyConfig = {
|
||||
plugins: [
|
||||
new CopyWebpackPlugin({
|
||||
|
|
@ -51,4 +59,5 @@ module.exports = [
|
|||
merge(common, appConfig, copyConfig),
|
||||
merge(common, backgroundConfig),
|
||||
merge(common, contentConfig),
|
||||
merge(common, injectConfig),
|
||||
];
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue