Greymonkey 插入 JavaScript

发布于 2024-09-03 03:27:31 字数 793 浏览 2 评论 0原文

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

受本教程的启发,我编写了上述书签。

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

请帮助我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半枫 2024-09-10 03:27:31

我们经常这样做。
这是一个适合您的脚本,只需编辑 @include 语句以匹配将使用 Greasemonkey 脚本的页面即可。

另外,/bookmark.php 可能需要更改为完整地址,而不是相对地址。

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

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;            \
    }                                               \
   '
);
末が日狂欢 2024-09-10 03:27:31

使其成为一个实际的命名函数,而不是一个闭包(就在 Greasemonkey 脚本中),然后将该函数添加为所述按钮的 onclick= 事件,直接内嵌在实际函数的下方。

尽管如此,为此目的劫持预先存在的网页按钮并不安全,也不是官方祝福的方法 - 将其附加到 Greasemonkey 菜单命令并从那里启动它(右键单击菜单的小猴子图标)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文