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?
// ==UserScript==
// @name Draggable box demo
// @namespace http://use.i.E.your.homepage/
// @version 0.1
// @description enter something useful
// @match *://www.*
// @copyright 2012+, You
// @require http://code.jquery.com/jquery-latest.js http://code.jquery.com/ui/1.9.2/jquery-ui.js
// ==/UserScript==
//alert("Hi!");
$(document).ready(function() {
$(document).append("<div id='dragZone'><div class='draggable'>Drag here!<input type = 'text'></input></div>");
$('#dragZone').css('position', 'absolute');
var a = 3;
$('.draggable').draggable({
start: function(event, ui) { $(this).css("z-index", a++); }
});
$('#dragZone div').mousedown(function() {
$(this).addClass('top').removeClass('bottom');
$(this).siblings().removeClass('top').addClass('bottom');
$(this).css("z-index", a++);
});
});
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
or
Here’s the fiddle for proof.
It’s probably the lack of the @require, maybe your greasemonkey is out of date?
Boilerplate template. Should work fine OOB.