https://github.com/robinvdvleuten/json-from-script
👮🏻 Tiny JSON parser for your CSP aware script tags.
https://github.com/robinvdvleuten/json-from-script
csp javascript json script security
Last synced: about 1 month ago
JSON representation
👮🏻 Tiny JSON parser for your CSP aware script tags.
- Host: GitHub
- URL: https://github.com/robinvdvleuten/json-from-script
- Owner: robinvdvleuten
- License: mit
- Created: 2017-05-12T07:34:41.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-09-08T14:32:16.000Z (over 5 years ago)
- Last Synced: 2025-02-16T06:32:41.288Z (12 months ago)
- Topics: csp, javascript, json, script, security
- Language: JavaScript
- Homepage: https://npm.im/json-from-script
- Size: 114 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# json-from-script
Tiny JSON parser for your [Content Security Policy](https://developers.google.com/web/fundamentals/security/csp/) aware script tags.
[](https://www.npmjs.com/package/json-from-script)
[](https://travis-ci.org/robinvdvleuten/json-from-script)
[](https://img.badgesize.io/https://cdn.jsdelivr.net/npm/json-from-script/dist/json-from-script.umd.js?compression=gzip)
[](https://packagephobia.now.sh/result?p=json-from-script)
## Installation
```
$ npm install --save json-from-script
```
## Usage
[**Real Example on JSFiddle**](https://jsfiddle.net/robinvdvleuten/9jk7L3vx/) ➡️
When your HTML contains any script tag like this;
```html
{"bar":"baz"}
```
You can parse it in your Javascript application like this;
```js
import jsonFromScript from 'json-from-script';
// Parsed will be { foo: { bar: 'baz' } }
const parsed = jsonFromScript();
```
## API
### `jsonFromScript(selector, attribute)`
Creates a new instance of the parser with the given options. The following options
can alternatively be provided to configure the parser for your specific needs:
- `selector `: The selector for the script tags to parse. (default: __script.data__)
- `attribute `: The data attribute to use when setting the attribute on the parsed object. (default: __data-attr__)
## License
MIT © [Robin van der Vleuten](https://www.robinvdvleuten.nl)