From 9b5284d457dbcf60c3db44f735dbe994730da780 Mon Sep 17 00:00:00 2001 From: jejolare Date: Sat, 22 Apr 2023 05:08:57 +0400 Subject: [PATCH] add inpage api --- public/manifest.json | 6 +++++ src/background/background.js | 6 ++--- src/content/content.js | 24 ++++++++++++++++++ src/content/inject.js | 47 ++++++++++++++++++++++++++++++++++++ webpack.config.js | 9 +++++++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/content/inject.js diff --git a/public/manifest.json b/public/manifest.json index 15d1fcf..7186650 100644 --- a/public/manifest.json +++ b/public/manifest.json @@ -22,6 +22,12 @@ { "matches": [""], "js": ["static/js/content.bundle.js"] + }, + { + "world": "MAIN", + "js": ["static/js/inject.bundle.js"], + "matches": [""], + "run_at": "document_start" } ], "icons": { diff --git a/src/background/background.js b/src/background/background.js index 3e796a0..de1e7c1 100644 --- a/src/background/background.js +++ b/src/background/background.js @@ -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) diff --git a/src/content/content.js b/src/content/content.js index e69de29..5faf39e 100644 --- a/src/content/content.js +++ b/src/content/content.js @@ -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'); \ No newline at end of file diff --git a/src/content/inject.js b/src/content/inject.js new file mode 100644 index 0000000..d952049 --- /dev/null +++ b/src/content/inject.js @@ -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(); \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 74ad047..a4cad82 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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), ];