运行的Greasemonkey脚本在同一页上,多次? [英] Run Greasemonkey script on the same page, multiple times?

查看:178
本文介绍了运行的Greasemonkey脚本在同一页上,多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全陌生的Greasemonkey,JavaScript的,其实所有的用户界面的东西。

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

要求:Userscript是在页面加载后运行一次由GS。不过,我需要相同的脚本进行无刷新多次运行

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

使用情况:对于离,Amazon.com的搜索偏偏使用Ajax。我需要嵌入在搜索结果中一个自定义的元素。

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

我需要我的内容的搜索结果,DIV随着每一个搜索发生在同一个页面时,结果注射(没有刷新页面)

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.

推荐答案

最简单,最可靠的方法是使用的waitForKeyElements()实用程序

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

下面是一个 完整的脚本使用jQuery和 waitForKeyElements 来改变亚马逊的搜索结果:

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);

这篇关于运行的Greasemonkey脚本在同一页上,多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆