1

I have a bookmarklet, clicking the bookmarklet includes a PHP script (evaluated as a JavaScript file) to the page few table values and select values passed as GET parameters. The PHP script writes the page data to the MySQL database, and outputs a success message that is treated as JavaScript code and executed by the browser. Is there any possibility to do this using greasemonkey and call this function when a existing button is clicked on the web page.

I wrote the above bookmarklet inspired by this tutorial.

http://tutorialzine.com/2010/04/simple-bookmarking-app-php-javascript-mysql/

This is the bookmarklet code:

(function () {
var jsScript = document.createElement('script');

jsScript.setAttribute('type', 'text/javascript');

jsScript.setAttribute('src', '/bookmark.php?url=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title));

document.getElementsByTagName('head')[0].appendChild(jsScript);
})();

Please help me.

2 Answers 2

2

We do this a lot. Here's a script that should work for you, just edit the @include statement to match the pages the Greasemonkey script will be used on.

Also, /bookmark.php will probably have to be changed to a full address, rather than a relative one.

//
// ==UserScript==
// @name            Adding a live button
// @namespace       http://www.google.com/
// @description     Adds a custom bookmarking button.
// @include         http://www.google.com/*
// ==/UserScript==
//


function LocalMain ()
{

    /*--- Create a button in a container div.  It will be styled and postioned with CSS.
    */
    var zNode       = document.createElement ('div');
    zNode.innerHTML = '<form id="idMyForm" method="get" action="">'
                    + '  <p><input type="submit" id="idMySubmitBtn" value="Bookmark it"></p>'
                    + '</form>'
                    ;
    zNode.setAttribute ('id', 'idBookMarkBtnContainer');

    document.body.appendChild (zNode);

    zNode.addEventListener ("submit", BookmarkButtonAction,   false);
}


function BookmarkButtonAction (zEvent)
{
    zEvent.preventDefault();

    var jsScript    = document.createElement('script');

    jsScript.setAttribute('type', 'text/javascript');

    /*--- Is "/bookmark.php" going to work on all target pages?
    */
    jsScript.setAttribute('src', '/bookmark.php?url=' + encodeURIComponent(location.href) + '&title=' + encodeURIComponent(document.title));

    document.getElementsByTagName('head')[0].appendChild(jsScript);

    return false;
}


window.addEventListener ("load", LocalMain, false);
//LocalMain();


GM_addStyle
(
   '#idBookMarkBtnContainer                         \
    {                                               \
        position:               absolute;           \
        top:                    0;                  \
        left:                   0;                  \
                                                    \
        background:             orange;             \
        border:                 3px double #999999; \
        margin:                 5px;                \
        opacity:                0.9;                \
        z-index:                222;                \
                                                    \
        min-height:             10px;               \
        min-width:              20px;               \
        padding:                5px 20px;           \
    }                                               \
    #idMySubmitBtn                                  \
    {                                               \
        cursor:                 pointer;            \
    }                                               \
   '
);
Sign up to request clarification or add additional context in comments.

Comments

0

Make this an actual named function, not a closure (right in Greasemonkey script), then add that function as an onclick= event of said button, straight inline just below the actual function.

Although, hijacking pre-existing webpage buttons for that purpose is not safe nor an officially blessed method - it's much better and easier to attach it to a Greasemonkey Menu Command and launch it from there (rightclick the small monkey icon for the menu).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.