如何使用 Tampermonkey 触发 jQuery 自动完成插件?

How to trigger a jQuery autocomplete plugin using Tampermonkey?

我正在尝试篡改一个具有自动完成输入文本框的网站。

当用户在其中一个文本框中输入内容时,它会触发 jQuery 搜索小部件,使用 jqueryui 1.8.2

我无法在 https://jqueryui.com/autocomplete/ 找到解决问题的方法。

我想做的是从我的用户脚本中取出一个字符串并将其插入其中一个文本框。然后我想自动触发 jQuery 搜索。我可以轻松地将字符串放入文本框中。

$('#website_Textbox').val('My String');

输入字符串后,我不知道如何触发 jquery 搜索。

我试过 .change().trigger('change') 我也试过聚焦文本框并单击文本框,但没有任何效果。

搜索它的唯一方法是按键。我试过创建一个事件 keypress,但也没有用。我无法使用 javascript 发送实际的按键。有人知道解决这个问题的方法吗?

只需使用JQUI's "search" methodDoc。例如:

$('#website_Textbox').val ('My String').autocomplete ("search", "My String");

现场演示:

var wordList = [
    "The", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog.",
    "Several", "fabulous", "dixieland", "jazz", "groups", "played", "with", "quick", "tempo.",
];
$("#zWords").autocomplete ( {source: wordList} );

$("button").click ( function () {
    $("#zWords").val ("er").autocomplete ("search", "er");
} );
<link href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>

<p><button>jump-start autocomplete</button></p>
<div class="ui-widget">
  <label for="zWords">Words: </label><input id="zWords">
</div>

或者在 Tampermonkey 脚本中:

// ==UserScript==
// @name     _autostart JQUI autocomplete value
// @match    *://YOUR_SERVER.COM/YOUR_PATH/*
// @grant    none
// ==/UserScript==

$('#website_Textbox').val ('My String').autocomplete ("search", "My String");

请注意,在某些情况下,使用 @grant 而不是 none 可能 需要脚本注入。