https://github.com/elbywan/yett
๐A small webpage library to control the execution of (third party) scripts
https://github.com/elbywan/yett
analytics block blocker gdpr privacy rgpd tracking
Last synced: 24 days ago
JSON representation
๐A small webpage library to control the execution of (third party) scripts
- Host: GitHub
- URL: https://github.com/elbywan/yett
- Owner: elbywan
- License: mit
- Created: 2018-06-04T10:03:05.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-05-11T09:55:02.000Z (12 months ago)
- Last Synced: 2024-05-19T12:20:33.780Z (12 months ago)
- Topics: analytics, block, blocker, gdpr, privacy, rgpd, tracking
- Language: JavaScript
- Homepage: https://elbywan.github.io/yett/
- Size: 458 KB
- Stars: 766
- Watchers: 41
- Forks: 71
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Yett
![]()
![]()
![]()
![]()
![]()
### ๐ A small webpage library to control the execution of (third party) scripts like analytics
##### Simply drop yett at the top of your html and it will allow you to block and delay the execution of other scripts.
## Background
[โ] **`So, why on Earth would I want to block scripts on my own website?`**
One way to use `yett` would be to build a [GDPR compliant consent-first-analytics](https://medium.com/snips-ai/gdpr-compliant-website-analytics-putting-users-in-control-684b17a1463f), via an UI like below.
![]()
Analytics scripts are blocked until users Accepts, (previously) in production at https://snips.ai
Blocking execution of analytics script (until consent is given) can be done manually, but the problem is that analytics providers often provide minified code embeds that you have to include in your html as they are. If you want to exercise control over their execution, then you have to tamper with this minified JS yourself, which is complex and does not scale well if you load several 3rd party scripts.
Another thing to consider is that these scripts first setup a local buffer that record user actions locally, and then upload the data only after a remote script is loaded asynchronously. Meaning that if the whole thing is simply wrapped inside a callback *(as some other libraries do)* then every action performed by the user on the web page before the callback gets executed won't get recorded and will never appear in your analytics dashboard.
Thus we invented `yett`. Just drop in the script and define a domain blacklist - `yett` will take care of the rest โจ.
------
And on a side note, it is technically quite amazing to know that **[a few lines of js](https://medium.com/snips-ai/how-to-block-third-party-scripts-with-a-few-lines-of-javascript-f0b08b9c4c0)** is all you need to control execution of other scripts, even those included with a script tag. ๐
##### *Also, [yett](https://en.wikipedia.org/wiki/Yett) has an interesting meaning.*
## Usage
#### [:tv: Demo](https://elbywan.github.io/yett/)
#### Small example
```html
window.YETT_BLACKLIST = [
/my-blacklisted-domain/,
]
// Or a whitelist
window.YETT_WHITELIST = [
/my-whitelisted-domain/,
]
// This one too
(function() {
var script = document.createElement('script')
script.setAttribute('src', 'https://my-blacklisted-domain.com/some-file.js')
script.setAttribute('type', 'application/javascript')
document.head.appendChild(script)
})()
Unblock
```
**โ ๏ธ It is strongly recommended (but not necessary) that you [add type attributes](https://github.com/elbywan/yett#add-a-type-attribute-manually) to `` tags having src attributes that you want to block. It has the benefit of preventing the scripts from begin downloaded in major browsers.**
**๐ก In any case, if you would like to ensure that cookies are not sent to third-party servers during the initial request you can use the [`crossorigin="anonymous"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) attribute. Check [this link](https://github.com/elbywan/yett/issues/20#issuecomment-599256485) for more details.**
## Add a blacklist
Yett needs a `blacklist`, which is an array of regexes to test urls against.
```html
<script>
// Add a global variable *before* yett is loaded.
YETT_BLACKLIST = [
/www\.google-analytics\.com/,
/piwik\.php/,
/cdn\.mxpnl\.com/
]
// OR
YETT_WHITELIST = [
/my-whitelisted-domain/
]```
### CDN
Finally, include `yett` with a script tag **before** other scripts you want to delay:
```html
```
Then, use `window.yett.unblock()` to resume execution of the blocked scripts.
### NPM
You can also use npm to install yett:
```bash
npm i yett
``````js
window.YETT_BLACKLIST = [
// ... //
]
// OR
window.YETT_WHITELIST = [
// ... //
]
// Side effects here! Do not import more than once!
import { unblock } from 'yett'unblock()
```### Unblock
```js
unblock(...scriptUrlsOrRegexes: (String | RegExp)[])
```> Unblocks blacklisted scripts.
If you don't specify a `scriptUrlsOrRegexes` argument, all the scripts that were previously blocked will be executed.
Otherwise, the `scriptUrlsOrRegexes` provided will be either removed from the blacklist or added to the whitelist and executed.### Build locally
```bash
# Clone
git clone https://github.com/elbywan/yett
cd yett
# Install
pnpm i
# Serves demo @ localhost:8080
pnpm dev
# Build for release
pnpm build
```## Browser compatibility
| | `` | `<script type="javascript/blocked">` | `document.createElement('script')` |
|------------------------|:-----------------------------------------------:|:-------------------------------------------:|:-------------------------------------------:|
| **Prevents loading** |  |  |  |
| **Prevents execution** |  |  |  |The most 'advanced' javascript feature that `yett` uses is [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver), which is compatible with all major browsers as well as `IE11`.
If you need `IE 9/10` compatibility, you will have to use a [polyfill](https://github.com/megawac/MutationObserver.js):
```html
<script src="https://cdn.jsdelivr.net/npm/mutationobserver-shim/dist/mutationobserver.min.js">
```### Caveats
#### Add a type attribute manually
Adding this attribute prevents the browser from downloading the script on `Chrome` and `Firefox`.
```html
```
#### Monkey patch
This library monkey patches `document.createElement`. No way around this.
This means that `yett` is not compatible with third-party browser extensions that also monkey patch this native browser function.
#### Dynamic requests
Scripts loaded using XMLHttpRequest and Fetch are not blocked. It would be trivial to monkey patch them, but most tracking scripts are not loaded using these 2 methods anyway.
## Suggestions
If you have any request or feedback for us feel free to open an [issue](https://github.com/elbywan/yett/issues)!
So far weโre using this library for analytics, but it could also be used to block advertising until consent, and other things we havenโt thought about yet. Weโre excited to see what use cases the community comes up with!
## License
MIT