Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/CoeJoder/waitForKeyElements.js
A utility function for userscripts that detects and handles AJAXed content.
https://github.com/CoeJoder/waitForKeyElements.js
Last synced: 16 days ago
JSON representation
A utility function for userscripts that detects and handles AJAXed content.
- Host: GitHub
- URL: https://github.com/CoeJoder/waitForKeyElements.js
- Owner: CoeJoder
- Created: 2020-05-01T21:07:49.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-28T07:28:21.000Z (5 months ago)
- Last Synced: 2024-08-01T00:46:09.315Z (3 months ago)
- Language: JavaScript
- Homepage:
- Size: 15.6 KB
- Stars: 78
- Watchers: 5
- Forks: 17
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- stars - CoeJoder/waitForKeyElements.js
README
# waitForKeyElements()
A utility function for userscripts that detects and handles AJAXed content.Forked from [the original](https://gist.github.com/BrockA/2625891) with major improvements, including:
- does not require jQuery
- avoids [the quirks](https://www.thecodeship.com/web-development/alternative-to-javascript-evil-setinterval/) associated with `setInterval()`
- optionally takes a function instead of a string for querying elements on page## Installation
Add the following to your userscript's metadata block:
```javascript
// @require https://cdn.jsdelivr.net/gh/CoeJoder/[email protected]/waitForKeyElements.js
```
If your userscript was already installed, you'll have to reinstall it to pickup the change. See [documentation](https://sourceforge.net/p/greasemonkey/wiki/Metadata_Block/#require).## Usage
### With selector string
```javascript
waitForKeyElements("div.comments", (element) => {
element.innerHTML = "This text inserted by waitForKeyElements().";
});
```
### With selector function
```javascript
waitForKeyElements(() => {
const iframe = document.querySelector('iframe');
if (iframe) {
const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
return iframeDoc.querySelectorAll("div.comments");
}
return null;
}, callbackFunc);
```## Additional Resources
If you need a more general purpose polling method, consider using the `wait()` function of [Greasemonkey Wrench](https://github.com/CoeJoder/GM_wrench), a toolbox of userscript utilities.