【问题标题】:userscript did run on greasemonkey addon用户脚本确实在greasemonkey插件上运行
【发布时间】:2011-02-12 09:36:09
【问题描述】:

我可以成功安装用户脚本[1];greasemonkey 和用户脚本都已明确启用;检查错误控制台没有发现异常。但是下面的脚本没有按预期运行(页面准备好后没有警报)。任何想法将不胜感激。

// ==UserScript==
// @name          clicksave
// @namespace     http://userscripts.org/users/pierr
// @description   click the words and it will be saved
// @copyright     pierr chen
// @contributor   pierr chen
// @include       http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js
// @license       GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
// @license       Creative Commons; http://creativecommons.org/licenses/by-nc-nd/3.0/
// @version       0.0.1
// ==/UserScript==

function getSelText()
{
    var txt = '';
     if (window.getSelection)
    {
        txt = window.getSelection();
             }
    else if (document.getSelection)
    {
        txt = document.getSelection();
            }
    else if (document.selection)
    {
        txt = document.selection.createRange().text;
            }
    else return;

    return txt
}

function saveSelText()
{
    var selText = getSelText()
    if (selText != "")
    {
        var url = "http://localhost:3000/auto_create?content="+getSelText();
        $.get(url,{},false);
    }
}

$(document).ready(function(){
    $(document).mouseup(function(){
        alert('Handler for .mouseup called.');  //even this alert will not jump out
        saveSelText()
    })

})

[1]http://userscripts.org/scripts/show/96771

【问题讨论】:

  • 我猜,jquery |当您调用 $(document); 时,$ 函数尚未加载;尝试在该行之前添加alert($);
  • Alert 不是调试器。使用 console.log() - 如果你安装了 FireBug。 ;)
  • @马克。添加没有产生任何差异

标签: javascript firefox greasemonkey userscripts


【解决方案1】:

原来应该使用 // @require @987654321@

某些版本的 jQuery 不会在 Greasemonkey 沙箱中运行。在撰写本文时,1.3.2 确实有效,但 1.4.1 无效。可以修补 1.4 版本的 jQuery 以工作,请参阅 jQuery 论坛:将 jQuery 1.4.1 导入到greasemonkey 脚本中会产生错误

【讨论】:

    猜你喜欢
    • 2012-02-05
    • 2021-03-29
    • 1970-01-01
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    相关资源
    最近更新 更多