-1

I'm trying to add a menu of buttons in a div to a page using jQuery from within a Greasemonkey script, but have had no luck so far, and I'm not sure exactly what I'm doing wrong:

// ==UserScript==
// @name        Case Email Utilities
// @namespace   CaseEmailUtil
// @description Checks everything over before closing
// @version     1
// @require     http://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js
// @grant       none
// @include     http://internalsite/*
// ==/UserScript==

"use strict";

this.$ = this.jQuery = jQuery.noConflict(true);

/**
 * A button that needs to be added to the ceu_bar
 * @param {string} name - This will be the id of the button.
 * @param {string} btn_text - The text to display on the button (the inner HTML).
 * @callback action - The function to execute when this button is pressed.
 * @param {string} title - Optional, if it's not provided, will default to the result of the action call back.
 */
function CEUButton(name, btn_text, action, title) {
    console.log("Creating CEUButton");
    var $ceu_btn = $('input[type=button]', {id: name}).html(btn_text).on('click', action);
    if ('undefined' === title) {
        $ceu_btn.attr("title", action());
    } else {
        $ceu_btn.attr("title", title);
    }
    console.log("CEUButton created");
    return $ceu_btn;
}

$(document).ready(function () {
    var isSendEmail = /^Task: Send an Email.*/;
    console.log("Readying CEU Bar");
    if (!isSendEmail.test(document.title)) {
        return;
    }

    // ceu: case email utilities bar
    //var ceu_bar = document.createElement('div');
    var $ceu_bar = $('div', {id: "ceu_bar"});
    $ceu_bar.append(CEUButton("test_btn", "Test Button", function () { return; }, "Click me!"));
    console.log("Prepending ceu_bar to the body...");
    $('body').prepend($ceu_bar);
    $ceu_bar.position({ my: "right", at: "right", of: "window" });
});

EDIT: I've taken out the unsafeWindow, and also set this.$ = this.jQuery = jQuery.noConflict(true); but I'm still not getting a menu bar to show up at all.

supercheetah
  • 3,200
  • 4
  • 25
  • 38
  • might have to use $(unsafeWindow.document).ready, but that unsafeWindow stuff always confuses me so i end up just using a few line GM userscript to inject an external script without special jargon. – dandavis Feb 14 '14 at 01:09
  • See ["jQuery in Greasemonkey 1.0 conflicts with websites using jQuery"](http://stackoverflow.com/q/12146445/331508) for starters. It also applies even if the target page does not use jQuery. ... Don't need `unsafeWindow.` in the posted code, either. – Brock Adams Feb 14 '14 at 01:45
  • You're right about the `unsafeWindow` and I've taken it out, but I've tried setting `this.$ = this.jQuery = jQuery.noConflict(true);` but I'm still not able to get this code to work. – supercheetah Feb 14 '14 at 21:23

1 Answers1

0

I was looking in the wrong place for the buttons on this. For some reason, they're showing in the upper left corner of the window rather than the right as I expected them to. I'm not sure how to fix that, though.

supercheetah
  • 3,200
  • 4
  • 25
  • 38