Add a draggable window to a page using Greasemonkey

后端 未结 2 762
悲&欢浪女
悲&欢浪女 2021-01-23 02:49

I\'m trying to create a Greasemonkey script that adds a draggable div to every web page. For some reason, the div isn\'t displaying at all. What might be the reason for this?

2条回答
  •  后悔当初
    2021-01-23 03:31

    Completely wrong on the first go - the issue is the use of $(document).append. You cannot append directly to the document, you can only append to a node.

    So Either

    $(document.body).append()
    

    or

    $('body').append()
    

    Here's the fiddle for proof.

    It's probably the lack of the @require, maybe your greasemonkey is out of date?

    // ==UserScript==
    // @name       My Fancy New Userscript
    // @namespace  http://use.i.E.your.homepage/
    // @version    0.1
    // @description  enter something useful
    // @match      http://*/*
    // @require    http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
    // @copyright  2012+, You
    // ==/UserScript==
    jQuery(function($){
        var _highest = 0;   
    
        $("div").each(function() {
            var _current = parseInt($(this).css("zIndex"), 10);
            if(_current > _highest) {
                _highest = _current + 1;
            }
        });
        $('body').append('
    Hello, This is an addon div from Greasemonkey.
    '); }); ​

    Boilerplate template. Should work fine OOB.

提交回复
热议问题