Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
22
rated 0 times [  26] [ 4]  / answers: 1 / hits: 19638  / 12 Years ago, mon, june 25, 2012, 12:00:00

I'm totally new to Greasemonkey, javascript, in fact all the UI stuff.



Requirement: Userscript is run by GS once after the page loads. However, I need the same script to be run multiple times without refresh



Use case: For ex, Amazon.com search happens using Ajax. I need to embed a custom element in the search results.



I need to inject my content to the search-results-div along with the results every time a search happens in the same page (there's no page refresh)



My current script runs only with the page refresh.



I hope above explanation is clear. Please help.


More From » ajax

 Answers
60

The simplest, most robust way is to use the waitForKeyElements() utility.



Here is a complete script that uses jQuery and waitForKeyElements 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==
/*- The @grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/

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

waitForKeyElements (#atfResults, addCustomSearchResult);

[#84672] Sunday, June 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shianncaylinc

Total Points: 502
Total Questions: 80
Total Answers: 105

Location: Botswana
Member since Sat, Jan 7, 2023
1 Year ago
;