Greasemonkey 脚本是否可以在 Firefox 中自动执行 Ctrl+A(全选)操作?

发布于 2024-09-11 09:12:51 字数 187 浏览 5 评论 0原文

是否可以编写一个 Greasemonkey 脚本来触发 Firefox 中的 Ctrl+A(全选)操作? (如果启用了脚本,加载新页面后?)

在任何可能的级别上帮助我。

更新:

“Firefox 有插件可以加速阅读或大声朗读所选文本。我只是希望自动化选择文本的部分。”

Is it possible to write a Greasemonkey script to trigger the Ctrl+A (select all) action in Firefox? (after a new page is loaded if the script is enabled?)

Help me at any level possible for you.

Update:

"Firefox has got addons to speed read or read aloud selected text. I just wish to automate the part where text is to be selected."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2024-09-18 09:12:51
var r = document.createRange()
r.selectNode(document.body)
window.getSelection().addRange(r)

我尝试创建一个新的 Greasemonkey 脚本,输入上面的代码(我从 此页面),并加载页面。

它确实选择了所有文本,但对于某些页面,它会立即取消选择。例如Google的主页,因为该页面聚焦于搜索领域。


BA 更新:

这在 Google 上不起作用,因为它与本机脚本作斗争。但是,通过在 onload 处重新运行代码以及之后再次运行代码,我们可以保留选择。

此外,如果本机脚本将焦点设置为 inputtextarea,我们必须与之对抗。

因此,包含所有这些想法并且似乎有效的 Greasemonkey 脚本是:

//--- Save this as "SelectWholePage.user.js" and install with Greasemonkey.
//
// ==UserScript==
// @name            Select a whole page
// @namespace       google.com
// @description     Selects a whole page (equivalent to 'Ctrl-A').
// @include         http://www.google.com/*
// ==/UserScript==
//

/*--- Run the main function 3 times (when DOM ready, at load and just after
    load) because page javascript will often reset the focus and selection.
*/
LocalMain ();
window.addEventListener
(
    "load",
    function(evt)
    {
        LocalMain ();
        window.setTimeout (LocalMain, 222);
    },
    false
);

function LocalMain ()
{
    var WholePage       = document.createRange ();
    WholePage.selectNode (document.body);

    window.getSelection ().addRange (WholePage);

    var aInputs         = document.getElementsByTagName ("input");

    for (var J = aInputs.length-1;  J>0;  J--)
        aInputs[J].blur ();

    document.body.focus ();
}
var r = document.createRange()
r.selectNode(document.body)
window.getSelection().addRange(r)

I tried creating a new Greasemonkey script, typing the above code (which I grabbed and edited from this page), and loading a page.

It did select all the text, but for some pages, it becomes unselected immediately. For example, Google's homepage, because the page focuses the search field.


Update by BA:

This didn't work on Google because it's fighting native scripts. But, by re-running the code at onload and again after, we can preserve the selection.

Also, if the native script sets focus to an input or textarea, we must fight that.

So, a Greasemonkey script that incorporates all these ideas, and seems to work is:

//--- Save this as "SelectWholePage.user.js" and install with Greasemonkey.
//
// ==UserScript==
// @name            Select a whole page
// @namespace       google.com
// @description     Selects a whole page (equivalent to 'Ctrl-A').
// @include         http://www.google.com/*
// ==/UserScript==
//

/*--- Run the main function 3 times (when DOM ready, at load and just after
    load) because page javascript will often reset the focus and selection.
*/
LocalMain ();
window.addEventListener
(
    "load",
    function(evt)
    {
        LocalMain ();
        window.setTimeout (LocalMain, 222);
    },
    false
);

function LocalMain ()
{
    var WholePage       = document.createRange ();
    WholePage.selectNode (document.body);

    window.getSelection ().addRange (WholePage);

    var aInputs         = document.getElementsByTagName ("input");

    for (var J = aInputs.length-1;  J>0;  J--)
        aInputs[J].blur ();

    document.body.focus ();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文