Skip to content

Commit

Permalink
fix error with running getUser immediately after initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Glench committed Feb 3, 2021
1 parent a058357 commit 7fa580c
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 11 deletions.
14 changes: 12 additions & 2 deletions ExtPay.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,20 @@ You can copy and paste this to your manifest.json file to fix this error:
}
}

// var paid_callbacks = [];
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetch_user() {
const storage = await get(['extensionpay_api_key', 'extensionpay_user'])
var storage = await get(['extensionpay_api_key', 'extensionpay_user'])
// wait 10 seconds for api key to be returned if creator is running this in background.js immediately after extpay initialization
for (var i=0; i < 20; ++i) {
if (storage.extensionpay_api_key) break;
await timeout(500)
storage = await get(['extensionpay_api_key', 'extensionpay_user'])
}
if (!storage.extensionpay_api_key) throw 'Error registering user.'

const resp = await fetch(`${EXTENSION_URL}/api/user?api_key=${storage.extensionpay_api_key}`, {
method: 'GET',
headers: {
Expand Down
14 changes: 12 additions & 2 deletions dist/ExtPay.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,20 @@ You can copy and paste this to your manifest.json file to fix this error:
}
}

// var paid_callbacks = [];
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetch_user() {
const storage = await get(['extensionpay_api_key', 'extensionpay_user']);
var storage = await get(['extensionpay_api_key', 'extensionpay_user']);
// wait 10 seconds for api key to be returned if creator is running this in background.js immediately after extpay initialization
for (var i=0; i < 20; ++i) {
if (storage.extensionpay_api_key) break;
await timeout(500);
storage = await get(['extensionpay_api_key', 'extensionpay_user']);
}
if (!storage.extensionpay_api_key) throw 'Error registering user.'

const resp = await fetch(`${EXTENSION_URL}/api/user?api_key=${storage.extensionpay_api_key}`, {
method: 'GET',
headers: {
Expand Down
14 changes: 12 additions & 2 deletions dist/ExtPay.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,20 @@ You can copy and paste this to your manifest.json file to fix this error:
}
}

// var paid_callbacks = [];
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetch_user() {
const storage = await get(['extensionpay_api_key', 'extensionpay_user']);
var storage = await get(['extensionpay_api_key', 'extensionpay_user']);
// wait 10 seconds for api key to be returned if creator is running this in background.js immediately after extpay initialization
for (var i=0; i < 20; ++i) {
if (storage.extensionpay_api_key) break;
await timeout(500);
storage = await get(['extensionpay_api_key', 'extensionpay_user']);
}
if (!storage.extensionpay_api_key) throw 'Error registering user.'

const resp = await fetch(`${EXTENSION_URL}/api/user?api_key=${storage.extensionpay_api_key}`, {
method: 'GET',
headers: {
Expand Down
14 changes: 12 additions & 2 deletions dist/ExtPay.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,20 @@ You can copy and paste this to your manifest.json file to fix this error:
}
}

// var paid_callbacks = [];
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetch_user() {
const storage = await get(['extensionpay_api_key', 'extensionpay_user']);
var storage = await get(['extensionpay_api_key', 'extensionpay_user']);
// wait 10 seconds for api key to be returned if creator is running this in background.js immediately after extpay initialization
for (var i=0; i < 20; ++i) {
if (storage.extensionpay_api_key) break;
await timeout(500);
storage = await get(['extensionpay_api_key', 'extensionpay_user']);
}
if (!storage.extensionpay_api_key) throw 'Error registering user.'

const resp = await fetch(`${EXTENSION_URL}/api/user?api_key=${storage.extensionpay_api_key}`, {
method: 'GET',
headers: {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "extpay",
"version": "1.1.0",
"version": "1.2.0",
"description": "The JavaScript library for https://extensionpay.com - payments for browser extensions, no server needed.",
"main": "./dist/ExtPay.common.js",
"module": "./dist/ExtPay.module.js",
Expand Down
14 changes: 12 additions & 2 deletions sample-extension/ExtPay.js
Original file line number Diff line number Diff line change
Expand Up @@ -1339,10 +1339,20 @@ You can copy and paste this to your manifest.json file to fix this error:
}
}

// var paid_callbacks = [];
function timeout(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function fetch_user() {
const storage = await get(['extensionpay_api_key', 'extensionpay_user']);
var storage = await get(['extensionpay_api_key', 'extensionpay_user']);
// wait 10 seconds for api key to be returned if creator is running this in background.js immediately after extpay initialization
for (var i=0; i < 20; ++i) {
if (storage.extensionpay_api_key) break;
await timeout(500);
storage = await get(['extensionpay_api_key', 'extensionpay_user']);
}
if (!storage.extensionpay_api_key) throw 'Error registering user.'

const resp = await fetch(`${EXTENSION_URL}/api/user?api_key=${storage.extensionpay_api_key}`, {
method: 'GET',
headers: {
Expand Down
4 changes: 4 additions & 0 deletions sample-extension/background.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
// this line is required in background.js to use ExtPay!
var extpay = ExtPay('sample-extension');

extpay.getUser().then(user => {
console.log(user)
})

0 comments on commit 7fa580c

Please sign in to comment.