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

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

问题描述

我完全不熟悉 Greasemonkey、javascript,实际上是所有 UI 的东西.

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

要求:用户脚本在页面加载后由 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天全站免登陆