Is it possible to execute a Greasemonkey script multiple times on a single page?

As someone who is new to Greasemonkey, JavaScript, and all things UI-related, I am seeking assistance.

I have a requirement where the Userscript needs to run once by Greasemonkey after the page loads. However, I also need the same script to be executed multiple times without refreshing the page.

For example, when performing a search on Amazon.com using Ajax, I want to include a custom element in the search results.

The challenge is to inject my content into the search-results-div along with the results every time a search is conducted on the same page without any page refreshes.

Currently, my script only works when the page is refreshed. I am hoping for guidance to make this work seamlessly.

Answer №1

If you're looking for a reliable and straightforward method, consider implementing the waitForKeyElements() utility.

Take a look at this comprehensive script that utilizes jQuery and waitForKeyElements to modify Amazon search outcomes:

// ==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==
/*- The @grant directive is required to circumvent a change in design
    brought about by GM 1.0. It restores the sandbox.
*/

function addCustomSearchResult (jNode) {
    //***** YOUR CODE HERE *****
    jNode.prepend (
        '<div id="result_000" class="fstRow">Buy my products instead!</div>'
    );
}

waitForKeyElements ("#atfResults", addCustomSearchResult);

Answer №2

To trigger your callback, you can utilize the DOMNodeInserted event like this:

document.addEventListener('DOMNodeInserted', function() { alert('hello') }, false);

Keep in mind that this event will respond to any alterations in the page structure, so make sure you are monitoring the correct changes.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The drop-down menu keeps flickering instead of remaining open when clicked

I am facing an issue with a dropdown menu in my webpage. The menu is initially hidden, but should become visible when the list element containing it is clicked. I have written JavaScript code to add a class that changes the visibility property to visible u ...

Converting Numbers into Words with the power of HTML and JavaScript

CONVERT NUMBER TO WORDS Write a simple JavaScript program that converts a number to words. Include an HTML page where the user can input the number to be converted. The conversion process should involve using both for loops and switch statements. ...

Error code EPERM encountered while attempting to append a file on a network

An issue arises when the application is required to store log data on a network drive. While everything works smoothly when the drive is hosted under Windows, complications arise when it is hosted on a Mac. Read/write operations function properly, but appe ...

Attempting to establish a connection with a MySQL database through the utilization of Node.js in combination with the mysql javascript client

Recently, I've been facing a challenge connecting to a MySQL database from my electron app: <script> var mysql = require('mysql'); var connection = mysql.createConnection({ host : '*********', ...

What is the best way to pass a variable in an AJAX function call when making a POST request?

I am facing an issue with sending a variable through an ajax function. The scenario is as follows: <a href="next.php" onclick="sendajax()">reload</a> I need to send this variable via the POST method in my php page: <script> fu ...

I am struggling to decide which attribute to use for implementing image swap on mouseover() and mouseout()

I have a problem using jQuery to switch between images when hovering on and off. Here's the code snippet I'm working with: HTML <img class="commentImg" src="images/comments.png" data-swap="images/comment_hover.png" alt=""> jQuery $(" ...

What is the best way to replace testcaferc.json browsers using the command line interface (CLI

Scenario: I am facing a situation where I aim to execute Testcafe in docker within a remote environment that necessitates running Testcafe through its command-line interface. I intend to utilize the .testcaferc file that I use for local testing to avoid m ...

vuejs props become null upon page refresh

MyComponent.vue template: <ResponsiveNavigation :nav-links="navLinks" /> script data: () => ({ navLinks: [] }), created: function() { this.getSocialMediaLinks(); }, methods: { getSocialMediaLinks() { var self = this; ...

What is the best way to distinguish a particular item in a <select> element and include a delete button for each row using jQuery?

1.) I've set up a nested table and now I want to ensure that when the 'Delete' button within the child table is clicked, its corresponding row gets deleted. 2.) I have a <select> tag. The issue is how can I implement validation to che ...

Determine whether the array of an object includes any elements from another array using Lodash

Each product in the array contains a unique structure that includes an ID, name, and materials list. The materials list consists of various materials with their own IDs and names. { id: 1, name: "product 1", materials: [ { id: 1, nam ...

How to efficiently transfer data between Node and Angular 7 using Electron

After setting up an Angular 7 application running on http://localhost:4200, I developed a Node JS application responsible for authenticating users on Facebook, accessible at http://localhost:3000. The callback redirection functions correctly within the No ...

Sending parameters to a function within the ngAfterViewInit() lifecycle method in Angular version 6

As a beginner in coding, I have created a canvas in Angular and am attempting to pass data received from my server to a function within ngAfterViewInit(). When I hard code the values I want to pass, everything works perfectly. However, if I try to pass da ...

Creating a website that adapts based on the subdomain used

Allow me to clarify my issue as best I can. If you need more details, please don't hesitate to ask, and forgive any errors in English as it is not my native language. Main Objective I am managing a website, www.mywebsite.com, that I intend to use fo ...

Using Asp.Net 4.0 Ajax Postback with Redirect causes the URL to be double escaped

After my session expired, I encountered an issue where clicking a button to trigger an Ajax Postback within an UpdatePanel would lead ASP to redirect me to the home page. However, the URL it redirected me to was escaped and ASP rejected the request. URL: ...

The submission of the form proceeds even with the warning displayed

I am facing an issue with a form that consists of one text field and a submit button. The user is required to input a 3-digit number, and if the number is greater than 200, a warning should be displayed. If the user clicks OK, the form should be submitted. ...

How to fetch a single document from a nested array using Mongoose

Currently, I am working on a MongoDB database using Mongoose in Node.js. The collection structure resembles the following: { cinema: 'xxx', account: 'yyy', media: { data: [{ id: 1, name: 'zzz& ...

What could be causing my AJAX form to refresh the page upon submission?

I have been working on a basic Follow/Unfollow system, and although the functionality is working correctly in terms of inserting and deleting rows when following/unfollowing, I'm facing an issue where the page refreshes every time despite using e.prev ...

Creating Conditional Vue Components

I am looking to dynamically create a list of actions, each of which is a component, based on a condition where the object variable store.plan is not empty. I attempted using v-if, which worked well for rendering but not for creating the component. However ...

VueJS added a new object to the array, but the data did not update reactively

Here is my current data layout days: [ { id: 0 name: 'Monday', times: [] }, { id: 1 name: 'Tuesday', times: [] } } I have implemented the following function to append an object to the times array. onT ...

AngularJS form submission with and without page refresh

Looking to implement an AngularJS form directive where, if on /home page, redirect to /search/term and if already on /search page, submit without refreshing the page by simply changing the location. I am able to do both separately, but struggling to writ ...