转换 JavaScript 书签以便与 Greasemonkey 一起使用?

发布于 2024-11-26 04:45:03 字数 735 浏览 0 评论 0原文

我正在尝试获取以下书签作为 Greasemonkey 脚本来解决 stackexchange 站点的可访问性错误。

javascript:(function(){$('a,%20.vote-up-off,%20.vote-down-off,%20.star-off').attr({role:'link',tabindex:'0'});})()

当我删除 function() 并将其放入以下 ​​Greasemonkey 脚本中时,它不起作用。

// ==UserScript==
// @name           StackExchange access
// @description    Enables y-aria stuff on stackoverflow
// @include *
// ==/UserScript==
$('a,%20.vote-up-off,%20.vote-down-off,%20.star-off').attr({role:'link',tabindex:'0'});
alert("worldzz");

我猜测我需要以某种方式从 Greasemonkey 访问文档对象,但不知道如何执行此操作。

我知道脚本被调用,因为如果我注释掉 $('a,%20.vote-up-off,%20.vote-down-off,%20.star-off').attr( {role:'link',tabindex:'0'}) 行我的警报被触发。

I'm trying to get the following bookmark to act as a Greasemonkey script to work around an accessibility bug with the stackexchange sites.

javascript:(function(){$('a,%20.vote-up-off,%20.vote-down-off,%20.star-off').attr({role:'link',tabindex:'0'});})()

When I remove the function() and put it in the following Greasemonkey script it does not work.

// ==UserScript==
// @name           StackExchange access
// @description    Enables y-aria stuff on stackoverflow
// @include *
// ==/UserScript==
$('a,%20.vote-up-off,%20.vote-down-off,%20.star-off').attr({role:'link',tabindex:'0'});
alert("worldzz");

I'm guessing that I need to access the document object somehow from Greasemonkey but am not sure how to do this.

I know the script is getting called because if I comment out the $('a,%20.vote-up-off,%20.vote-down-off,%20.star-off').attr({role:'link',tabindex:'0'}) line my alert gets hit.

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

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

发布评论

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

评论(2

稚气少女 2024-12-03 04:45:03
  1. Greasemonkey 脚本无法直接访问 jQuery。 Greasemonkey 将脚本沙箱到其自己的范围内,因此您需要在页面全局范围内访问的任何内容(例如 jQuery)都必须通过 window.wrappedJSObject 访问
  2. 您需要替换所有 %20 在 Greasemonkey 版本中带有空格字符
  3. 您需要将 jQuery DOM 操作放入 Ready 函数中,以便在元素出现在屏幕上之前它不会运行。

代码:

// ==UserScript==
// @name           StackExchange access
// @description    Enables y-aria stuff on stackoverflow
// @include *
// ==/UserScript==
( function( global )
{
    var $;

    if( global.jQuery )
    {
        $ = global.jQuery;

        $( function()
        {
            $( 'a, .vote-up-off, .vote-down-off, .star-off' )
                .attr( {
                    role:'link',
                    tabindex:'0'
                } );
        } );
    }
}( window.wrappedJSObject ) );
  1. A greasemonkey script will not have access to jQuery directly. Greasemonkey sandboxes the script into its own scope so anything you need to access in the page's global scope (like jQuery) has to be accessed via window.wrappedJSObject
  2. You need to replace all %20 in your Greasemonkey version with a space char
  3. You need to put the jQuery DOM manipulation in the ready function so that it doesn't run until the elements are on screen.

Code:

// ==UserScript==
// @name           StackExchange access
// @description    Enables y-aria stuff on stackoverflow
// @include *
// ==/UserScript==
( function( global )
{
    var $;

    if( global.jQuery )
    {
        $ = global.jQuery;

        $( function()
        {
            $( 'a, .vote-up-off, .vote-down-off, .star-off' )
                .attr( {
                    role:'link',
                    tabindex:'0'
                } );
        } );
    }
}( window.wrappedJSObject ) );
情独悲 2024-12-03 04:45:03
  1. 由于 Greasemonkey 脚本不会对其源代码进行 URL 解码,因此您需要将所有 %20 替换为 空格 字符。

  2. 然后,要访问页面的 jQuery,如果页面有它,只需使用:

    // ==用户脚本==
    // @name StackExchange 访问
    // @description 在 stackoverflow 上启用 y-aria 内容
    // @包括 *
    // ==/用户脚本==
    unsafeWindow.$('a, .vote-up-off, .vote-down-off, .star-off').attr({role:'link', tabindex:'0'});
    警报(“worldzz”);
    

1.注意:这两种方法,尤其是 JAAulde 的答案,都存在轻微风险该网页可以攻击您的系统



一种替代方法,(1) 没有安全风险,并且 (2) 适用于不使用 jQuery 的页面;是让 GM 脚本使用它自己的 jQuery。

这样做:

// ==UserScript==
// @name            StackExchange access
// @description     Enables y-aria stuff on stackoverflow
// @include *
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==
$ ('a, .vote-up-off, .vote-down-off, .star-off').attr({role:'link', tabindex:'0'});
alert("worldzz");
  1. Because a Greasemonkey script does not URL decode its source, you need to replace all %20 with the space character.

  2. Then, to access the page's jQuery, if the page even has it, just use:

    // ==UserScript==
    // @name           StackExchange access
    // @description    Enables y-aria stuff on stackoverflow
    // @include *
    // ==/UserScript==
    unsafeWindow.$ ('a, .vote-up-off, .vote-down-off, .star-off').attr({role:'link', tabindex:'0'});
    alert("worldzz");
    

1. Note: Both this method, and especially JAAulde's answer, carry a slight risk that the web page can pwn your system.



An alternate method, (1) without the security risk, and that (2) works on pages that don't use jQuery; is for the GM script to use it's own jQuery.

Do that like so:

// ==UserScript==
// @name            StackExchange access
// @description     Enables y-aria stuff on stackoverflow
// @include *
// @require         http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// ==/UserScript==
$ ('a, .vote-up-off, .vote-down-off, .star-off').attr({role:'link', tabindex:'0'});
alert("worldzz");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文