Skip to content

Commit a0223e6

Browse files
committed
Fix cross-site scripting issues and version bump to 0.1.1
1 parent 8ad8142 commit a0223e6

File tree

8 files changed

+91
-82
lines changed

8 files changed

+91
-82
lines changed

README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,29 @@ The _accept-terms_ plugin is only loaded for the checkout contact information st
1212

1313
To enable a plugin add this code to your _Google Analytics Additional Scripts_ section in Shopify's Admin / Online Store / Preferences.
1414

15-
```
16-
(function() {
15+
```javascript
16+
fetch('//cdn.jsdelivr.net/gh/nerdsofalltrades/[email protected]/dist/shopify-plugin-loader.min.js').then(function(
17+
result
18+
) {
19+
result.text().then(function(script) {
1720
var plugin = document.createElement('script');
18-
plugin.src = '//cdn.jsdelivr.net/gh/nerdsofalltrades/[email protected]/dist/shopify-plugin-loader.min.js';
21+
plugin.innerHTML = script;
1922
document.body.appendChild(plugin);
2023

21-
plugin.onload = function () {
22-
var ctx = window.ShopifyPlugins;
23-
24-
// Load plugins here
24+
// Load plugins here
25+
var ctx = window.ShopifyPlugins;
2526

26-
// Load the accept-terms plugin only in checkout
27-
// contact information step
28-
ctx.checkout.contactInformation.load('accept-terms');
27+
// Load the accept-terms plugin only in checkout
28+
// contact information step
29+
ctx.checkout.contactInformation.load('accept-terms');
2930

30-
// Load the progress-bar plugin for all steps
31-
// in checkout
32-
ctx.checkout.all.load('progress-bar');
31+
// Load the progress-bar plugin for all steps
32+
// in checkout
33+
ctx.checkout.all.load('progress-bar');
3334

34-
// Load other plugins here...
35-
}
36-
})();
35+
// Load other plugins...
36+
});
37+
});
3738
```
3839

3940
## Plugins
@@ -45,26 +46,26 @@ Checkbox in Shopify's contact information checkout step.
4546

4647
![Accept terms plugin in action](examples/accept-terms/accept-terms.png)
4748

48-
Add this code to your `plugin.onload` function to enable it.
49+
Add this code to enable it.
4950

50-
```
51+
```javascript
5152
ctx.checkout.contactInformation.load('accept-terms');
5253
```
5354

5455
Without options standard english texts are displayed and the url of your terms
5556
is expected to be found at `/pages/terms`. To change that just pass options
5657
and set it up as you like.
5758

58-
```
59+
```javascript
5960
ctx.checkout.contactInformation.load('accept-terms', {
6061
// The checkbox label
61-
label: "I have read and I agree to the",
62+
label: 'I have read and I agree to the',
6263
// The label of the terms link
63-
termsName: "terms",
64+
termsName: 'terms',
6465
// The url to your terms
65-
termsURL: "/pages/terms",
66+
termsURL: '/pages/terms',
6667
// Message displayed when customer tries to go on without agreeing
67-
errorMessage: "Please agree to our terms before your purchase"
68+
errorMessage: 'Please agree to our terms before your purchase'
6869
});
6970
```
7071

@@ -77,15 +78,15 @@ configuration is needed.
7778

7879
![Progress bar plugin in action](examples/progress-bar/progress-bar.png)
7980

80-
Add this code to your `plugin.onload` function to enable it.
81+
Add this code to enable it.
8182

82-
```
83+
```javascript
8384
ctx.checkout.all.load('progress-bar');
8485
```
8586

8687
## Development
8788

88-
```
89+
```shell
8990
$ npm install
9091
$ npm run dev
9192
```

dist/accept-terms.html

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,21 @@ <h2>Shipping address</h2>
4747
</script>
4848
<script>
4949

50-
var plugin = document.createElement('script');
51-
plugin.src = 'shopify-plugin-loader.min.js';
52-
53-
document.body.appendChild(plugin);
54-
55-
plugin.onload = function () {
56-
var ctx = window.ShopifyPlugins;
57-
ctx.checkout.contactInformation.load('accept-terms', {
58-
label: "I have read and I agree to the",
59-
termsName: "terms",
60-
termsURL: "/pages/terms",
61-
errorMessage: "Please agree to our terms before your purchase"
62-
});
63-
}
50+
fetch('shopify-plugin-loader.min.js').then(
51+
function (result) {
52+
result.text().then(function (script) {
53+
var plugin = document.createElement('script');
54+
plugin.innerHTML = script;
55+
document.body.appendChild(plugin);
56+
window.ShopifyPlugins.checkout.contactInformation.load('accept-terms', {
57+
label: "I have read and I agree to the",
58+
termsName: "terms",
59+
termsURL: "/pages/terms",
60+
errorMessage: "Please agree to our terms before your purchase"
61+
});
62+
})
63+
}
64+
);
6465
</script>
6566
</body>
6667

dist/progress-bar.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ <h1>Progress Bar</h1>
6969
}
7070
</script>
7171
<script>
72-
73-
var plugin = document.createElement('script');
74-
plugin.src = 'shopify-plugin-loader.min.js';
75-
76-
document.body.appendChild(plugin);
77-
78-
plugin.onload = function () {
79-
var ctx = window.ShopifyPlugins;
80-
ctx.checkout.all.load('progress-bar');
81-
}
72+
fetch('shopify-plugin-loader.min.js').then(
73+
function (result) {
74+
result.text().then(function (script) {
75+
var plugin = document.createElement('script');
76+
plugin.innerHTML = script;
77+
document.body.appendChild(plugin);
78+
window.ShopifyPlugins.checkout.all.load('progress-bar');
79+
})
80+
}
81+
);
8282
</script>
8383
</body>
8484

dist/shopify-plugin-loader.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/accept-terms/accept-terms.html

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,21 @@ <h2>Shipping address</h2>
4747
</script>
4848
<script>
4949

50-
var plugin = document.createElement('script');
51-
plugin.src = 'shopify-plugin-loader.min.js';
52-
53-
document.body.appendChild(plugin);
54-
55-
plugin.onload = function () {
56-
var ctx = window.ShopifyPlugins;
57-
ctx.checkout.contactInformation.load('accept-terms', {
58-
label: "I have read and I agree to the",
59-
termsName: "terms",
60-
termsURL: "/pages/terms",
61-
errorMessage: "Please agree to our terms before your purchase"
62-
});
63-
}
50+
fetch('shopify-plugin-loader.min.js').then(
51+
function (result) {
52+
result.text().then(function (script) {
53+
var plugin = document.createElement('script');
54+
plugin.innerHTML = script;
55+
document.body.appendChild(plugin);
56+
window.ShopifyPlugins.checkout.contactInformation.load('accept-terms', {
57+
label: "I have read and I agree to the",
58+
termsName: "terms",
59+
termsURL: "/pages/terms",
60+
errorMessage: "Please agree to our terms before your purchase"
61+
});
62+
})
63+
}
64+
);
6465
</script>
6566
</body>
6667

examples/progress-bar/progress-bar.html

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@ <h1>Progress Bar</h1>
6969
}
7070
</script>
7171
<script>
72-
73-
var plugin = document.createElement('script');
74-
plugin.src = 'shopify-plugin-loader.min.js';
75-
76-
document.body.appendChild(plugin);
77-
78-
plugin.onload = function () {
79-
var ctx = window.ShopifyPlugins;
80-
ctx.checkout.all.load('progress-bar');
81-
}
72+
fetch('shopify-plugin-loader.min.js').then(
73+
function (result) {
74+
result.text().then(function (script) {
75+
var plugin = document.createElement('script');
76+
plugin.innerHTML = script;
77+
document.body.appendChild(plugin);
78+
window.ShopifyPlugins.checkout.all.load('progress-bar');
79+
})
80+
}
81+
);
8282
</script>
8383
</body>
8484

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "shopify-plugins",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "Add features to your Shopify storefront and checkout with lightweight and easily integrateable plugins.",
55
"main": "src/loader.js",
66
"scripts": {

src/loader.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
class Loader {
22
static load(plugin, options) {
3-
const script = document.createElement('script');
4-
script.src = `shopify-plugin-${plugin}.min.js`;
5-
script.onload = function() {
6-
window.ShopifyPlugins.plugins[plugin](options);
7-
};
3+
const origin =
4+
process.env.NODE_ENV === 'production'
5+
? `//cdn.jsdelivr.net/gh/nerdsofalltrades/shopify-plugins@${process.env.VERSION}/dist/`
6+
: '';
87
process.env.NODE_ENV === 'development' && console.log(`Lazy-Loading ${plugin} plugin...`);
9-
document.body.appendChild(script);
8+
fetch(`${origin}shopify-plugin-${plugin}.min.js`).then(result => {
9+
result.text().then(script => {
10+
const code = document.createElement('script');
11+
code.innerHTML = script;
12+
document.body.appendChild(code);
13+
window.ShopifyPlugins.plugins[plugin](options);
14+
});
15+
});
1016
}
1117
}
1218

0 commit comments

Comments
 (0)