Home >Web Front-end >JS Tutorial >How Can I Execute a Greasemonkey Script Multiple Times on the Same Page Without Refreshing?
Executing Greasemonkey Scripts Multiple Times Per Page
As a new user to Greasemonkey and web development, you encounter a challenge: running a userscript multiple times on the same page without refreshing the page. This is necessary for scenarios like Ajax-driven Amazon searches, where you aim to insert a custom element into search results as they appear.
The effective solution recommended by experienced users is the waitForKeyElements() utility. It allows you to specify a target element on the page and register a callback function that will execute every time the target element is added or modified.
To demonstrate its usage, let's consider a modified script for altering Amazon search results:
// Greasemonkey script to alter Amazon search results // ==UserScript== // @name _Amazon Search, alter results // @include http://www.amazon.com/s/* // @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js // @require https://gist.github.com/raw/2625891/waitForKeyElements.js // @grant GM_addStyle // ==/UserScript== function addCustomSearchResult (jNode) { // Inject your custom element jNode.prepend ( '<div>
Here's how it works:
The above is the detailed content of How Can I Execute a Greasemonkey Script Multiple Times on the Same Page Without Refreshing?. For more information, please follow other related articles on the PHP Chinese website!