小编典典

在同一页面上多次运行Greasemonkey脚本?

ajax

我完全不熟悉Greasemonkey,JavaScript,实际上是所有UI东西。

要求:页面加载后,用户脚本由GS运行一次。但是,我需要多次运行同一脚本而不刷新

用例:例如,Amazon.com搜索使用Ajax进行。我需要在搜索结果中嵌入自定义元素。

每次在同一页面上进行搜索时,我都需要将内容和结果一起注入到search-results-div中(没有页面刷新)

我当前的脚本仅在页面刷新时运行。

我希望以上解释清楚。请帮忙。


阅读 231

收藏
2020-07-26

共1个答案

小编典典

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

这是一个使用jQuery并更改Amazon搜索结果的 完整脚本waitForKeyElements

// ==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);
2020-07-26