https://github.com/binki/binki-userscript-when-element-changed-async
A whenElementChangedAsync() function for use in userscripts (Greasemonkey).
https://github.com/binki/binki-userscript-when-element-changed-async
Last synced: 11 months ago
JSON representation
A whenElementChangedAsync() function for use in userscripts (Greasemonkey).
- Host: GitHub
- URL: https://github.com/binki/binki-userscript-when-element-changed-async
- Owner: binki
- License: mit
- Created: 2022-03-13T05:14:22.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-13T05:15:58.000Z (over 4 years ago)
- Last Synced: 2025-02-28T12:40:36.387Z (over 1 year ago)
- Language: JavaScript
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## Usage
Include this in your userscript using [`@require`](https://wiki.greasespot.net/Metadata_Block#.40require). It is recommended to [use a permalink](https://docs.github.com/en/repositories/working-with-files/using-files/getting-permanent-links-to-files) instead of referring to `master`.
```js
// ==UserScript==
// @name example
// @version 1.0
// @require https://github.com/binki/binki-userscript-when-element-changed-async/raw/master/binki-userscript-when-element-changed-async.js
// ==UserScript==
(async () => {
const soughtElement = await (async () => {
while (true) {
const e = document.querySelector('*[role=textbox][spellcheck=true][contenteditable=true].notranslate');
if (e) return e;
await whenElementChangedAsync(document.body);
}
})();
console.log(`Found element`, soughtElement);
})();
```
## API
```js
whenElementChangedAsync(target, options);
```
Parameters:
* `target` is the `Element` passed directly to [`MutationObserver.observe()`](https://dom.spec.whatwg.org/#dom-mutationobserver-observe). It must be specified and represents the element to monitor for changes.
* `options` is the [`MutationObserverInit`](https://dom.spec.whatwg.org/#dictdef-mutationobserverinit) passed directly to [`MutationObserver.observe()`](https://dom.spec.whatwg.org/#dom-mutationobserver-observe). If unspecified, a default which specifies `childList`, `attributes`, `characterData` shall be used. However, this is nonperformant and should be avoided when possible.
Returns:
A `Promise` which resolves to an array of [`MutationRecord`](https://dom.spec.whatwg.org/#mutationrecord) once a mutation happens.