greasemonkey插入javascript [英] greasemonkey insert javascript

查看:60
本文介绍了greasemonkey插入javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个书签,点击书签包含一个PHP脚本(评估为JavaScript文件)到页面的几个表值,并选择作为GET参数传递的值。 PHP脚本将页面数据写入MySQL数据库,并输出成功消息,该消息被视为JavaScript代码并由浏览器执行。是否有可能使用greasemonkey执行此操作,并在网页上单击现有按钮时调用此函数。

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/

这是书签代码:

(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);
})();

请帮帮我。

推荐答案

我们做了很多。
这是一个应该适合您的脚本,只需编辑 @include 语句以匹配将使用Greasemonkey脚本的页面。

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.

此外, /bookmark.php 可能必须更改为完整地址,而不是相对地址。

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;            \
    }                                               \
   '
);

这篇关于greasemonkey插入javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆