[英]Run Greasemonkey script on the same page, multiple times?
我是Greasemonkey,javascript的新手,實際上是所有UI的東西。
要求:用戶腳本在頁面加載后由GS運行一次。 但是,我需要在不刷新的情況下多次運行相同的腳本
使用案例:例如,使用Ajax進行Amazon.com搜索。 我需要在搜索結果中嵌入自定義元素。
每次在同一頁面中進行搜索時,我都需要將我的內容注入search-results-div以及結果(沒有頁面刷新)
我當前的腳本僅在頁面刷新時運行。
我希望上面的解釋清楚。 請幫忙。
最簡單,最健壯的方法是使用waitForKeyElements()實用程序 。
這是一個完整的腳本 ,它使用jQuery和waitForKeyElements
來改變Amazon搜索結果:
// ==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);
您可以使用DOMNodeInserted
事件來調用您的回調,例如:
document.addEventListener('DOMNodeInserted', function() { alert('hi') }, false);
請注意,該事件將由頁面結構中的任何更改觸發,因此您必須檢查是否定位了正確的更改。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.