Skip to content

Commit

Permalink
2.2.1 - only need content script for onPaid callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
Glench committed May 26, 2021
1 parent e3ae544 commit 45a3cc6
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 146 deletions.
55 changes: 28 additions & 27 deletions ExtPay.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,6 @@ You can copy and paste this to your manifest.json file to fix this error:
`
}

const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`
const manifest = browser.runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
})
if (!extpay_content_script_entry) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: Please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}
})
// ----- end configuration checks

Expand Down Expand Up @@ -244,6 +217,34 @@ ${content_script_template}`
},
onPaid: {
addListener: function(callback) {
const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`
const manifest = browser.runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
})
if (!extpay_content_script_entry) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: To use the onPaid callback handler, please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}

paid_callbacks.push(callback)
},
// removeListener: function(callback) {
Expand Down
28 changes: 18 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ ExtPay needs the following configuration in your V2 `manifest.json`:
```json
{
"manifest_version": 2,
"content_scripts": [
{
"matches": ["https://extensionpay.com/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}
],
"permissions": [
"storage"
]
Expand All @@ -43,8 +36,6 @@ ExtPay needs the following configuration in your V2 `manifest.json`:

ExtPay will not show a scary permission warning when users try to install your extension.

The content script is required to enable `extpay.onPaid` callbacks (see below). If you're using a bundler, you can create a file called something like `ExtPay_content_script.js` that only contains `import 'ExtPay'` or `require('ExtPay')` and use that in the `"js"` field above.

If you have a `"content_security_policy"` in your manifest or get a `Refused to connect to 'https://extensionpay.com...'` error, you'll have to add `connect-src https://extensionpay.com` to your extension's content security policy. <a href="https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_security_policy">See Mozilla's documentation for more details</a>.

Manifest V3 support coming soon.
Expand Down Expand Up @@ -137,9 +128,26 @@ extpay.onPaid.addListener(user => {
})
```

To use this feature, you will need to include the following content script configuration in your `manifest.json`:

```json
{
"manifest_version": 2,
"content_scripts": [
{
"matches": ["https://extensionpay.com/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}
]
}
```

The content script is required to enable `extpay.onPaid` callbacks. It will add a permissions warning when installing your extension. If you're using a bundler, you can create a file called something like `ExtPay_content_script.js` that only contains `import 'ExtPay'` or `require('ExtPay')` and use that in the `"js"` field above.

You can add as many callback functions as you want.

Note: `onPaid` callbacks will be called after a user pays as well as after a user "logs in" (e.g. activates their paid account on a different browser/profile/install). This may change in the future. If you'd like this to work differently, please contact me with a detailed explanation of your use case :)
Note: `onPaid` callbacks will be called after a user pays as well as after a user "logs in" (e.g. activates their paid account on a different browser/profile/install). This may change in the future -- if you'd like this to work differently, please contact me with a detailed explanation of your use case :)


## 7. Use `extpay.openPaymentPage()` to let the user manage their subscription preferences
Expand Down
55 changes: 28 additions & 27 deletions dist/ExtPay.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,33 +54,6 @@ You can copy and paste this to your manifest.json file to fix this error:
`
}

const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`;
const manifest = browser.runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
});
if (!extpay_content_script_entry) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: Please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}
});
// ----- end configuration checks

Expand Down Expand Up @@ -244,6 +217,34 @@ ${content_script_template}`
},
onPaid: {
addListener: function(callback) {
const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`;
const manifest = browser.runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
});
if (!extpay_content_script_entry) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: To use the onPaid callback handler, please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}

paid_callbacks.push(callback);
},
// removeListener: function(callback) {
Expand Down
55 changes: 28 additions & 27 deletions dist/ExtPay.js
Original file line number Diff line number Diff line change
Expand Up @@ -1283,33 +1283,6 @@ You can copy and paste this to your manifest.json file to fix this error:
`
}

const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`;
const manifest = browserPolyfill.runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
});
if (!extpay_content_script_entry) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: Please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}
});
// ----- end configuration checks

Expand Down Expand Up @@ -1473,6 +1446,34 @@ ${content_script_template}`
},
onPaid: {
addListener: function(callback) {
const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`;
const manifest = browserPolyfill.runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
});
if (!extpay_content_script_entry) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: To use the onPaid callback handler, please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}

paid_callbacks.push(callback);
},
// removeListener: function(callback) {
Expand Down
55 changes: 28 additions & 27 deletions dist/ExtPay.module.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,6 @@ You can copy and paste this to your manifest.json file to fix this error:
`
}

const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`;
const manifest = runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
});
if (!extpay_content_script_entry) {
throw `ExtPay setup error: Please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: Please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}
});
// ----- end configuration checks

Expand Down Expand Up @@ -242,6 +215,34 @@ ${content_script_template}`
},
onPaid: {
addListener: function(callback) {
const content_script_template = `"content_scripts": [
{
"matches": ["${HOST}/*"],
"js": ["ExtPay.js"],
"run_at": "document_start"
}]`;
const manifest = runtime.getManifest();
if (!manifest.content_scripts) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
const extpay_content_script_entry = manifest.content_scripts.find(obj => {
// removing port number because firefox ignores content scripts with port number
return obj.matches.includes(HOST.replace(':3000', '')+'/*')
});
if (!extpay_content_script_entry) {
throw `ExtPay setup error: To use the onPaid callback handler, please include ExtPay as a content script in your manifest.json matching "${HOST}/*". You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
} else {
if (!extpay_content_script_entry.run_at || extpay_content_script_entry.run_at !== 'document_start') {
throw `ExtPay setup error: To use the onPaid callback handler, please make sure the ExtPay content script in your manifest.json runs at document start. You can copy the example below into your manifest.json or check the docs: https://github.com/Glench/ExtPay#2-configure-your-manifestjson
${content_script_template}`
}
}

paid_callbacks.push(callback);
},
// removeListener: function(callback) {
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": "2.2.0",
"version": "2.2.1",
"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
Loading

0 comments on commit 45a3cc6

Please sign in to comment.