简体   繁体   English

如何调用从GreaseMonkey脚本添加到jQuery对象的函数?

[英]How can I call a functions added to the jQuery Object from my GreaseMonkey script?

I've created a GreaseMonkey script based on an in-page script that I'd like to use on a site , however I'm having the following issue: 我已经基于要在网站使用的页内脚本创建了GreaseMonkey脚本,但是存在以下问题:

In the script, we've created our namespace ( brickJax ) that contains the bulk of the functions, and I've required jQuery as we're using the jQuery replaceText functions as well. 在脚本中,我们创建了包含大部分函数的命名空间( brickJax ),并且在使用jQuery replaceText函数的同时,我还需要jQuery。

When I call the replaceText function from replaceSet I get the following error on the console: 当我从replaceSet调用replaceText函数时,在控制台上出现以下错误:

uncaught exception: TypeError: $(node).replaceText is not a function 未捕获的异常:TypeError:$(node).replaceText不是函数

However, calling it as part of the GM_xmlhttpRequest onload callback works fine. 但是,将其作为GM_xmlhttpRequest onload回调的一部分调用可以正常工作。

var brickJax = (function ($) {
  "use strict";
  var brickJax = {};

  //[Code here]

  function replaceSet(node, str, number, colour) {
    var text = '<a href="http://www.peeron.com/inv/sets/' + number + '-1">'
               + number + ' on Peeron</a>';

    // THIS LINE THROWS THE ERROR:
    $(node).replaceText(str, text);
  }

  function replaceImage(node, str, number, colour) {
    getBricksForImage(number, colour, node, str);
  }

  function getBricksForImage(number, colour, node, str) {
    GM_xmlhttpRequest({
        method: "POST",
        url: "http://brickjax.doodle.co.uk/bricks.aspx/JsonDetails/" + number 
             + "/" + colour,
        dataType: "jsonp",
        onload: function (data) {
            // THIS CALL WORKS PERFECTLY
            $(node).replaceText(str,
                                buildImage($.parseJSON(data.responseText)));
        }
    });
  };

  function buildImage(ajaxData) {
    var text = '<img style="max-height:100px;" src="' + ajaxData.Src 
               + '" alt="' + ajaxData.AltText + '" />';

    return text;
  }

  function replaceTags(element) {
    var elements = $(element).find('*').andSelf();
    elements = elements.not($('script,noscript,style,textarea,pre,code')
                       .find('*').andSelf());

    searchText(elements, /\[part:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceImage);

    searchText(elements, /\[set:([\w\-]*)(?::([\w\-]*))?\]/gi, replaceSet);
  };

})(jQuery);

brickJax.replaceTags($('body'));

(function ($) { $.fn.replaceText = function (b, a, c) { [...] } })(jQuery);

In the actual script I've added logging, which shows that node is an HTML element in both cases. 在实际的脚本中,我添加了日志记录,这表明在两种情况下该node都是HTML元素。

What is it that I'm doing wrong in the call from replaceSet that is different from the call in callback that's causing this error? 我在来自replaceSet的调用中做错了什么,这与导致此错误的回调中的调用不同?

In the hosted version both calls work as expected. 托管版本中,两个调用均按预期方式工作。

Apologies for the wall of script, I've tried to cut it down to the bare essentials. 对脚本墙表示歉意,我尝试将其简化为基本内容。

This is either closure related or due to the function being defined in an expression versus a declaration . 这与闭包有关,或者是由于表达式声明中定义函数

Either way, the solution should be the same, move the definition of replaceText physically before the var brickJax = ... stuff. 无论哪种方式,解决方案都应该相同,将replaceText的定义物理地移到var brickJax = ...东西之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Greasemonkey-按钮已添加到 <div> 我添加了,而不是调用我的oilmonkey函数 - Greasemonkey - Button Added to a <div> I added, not calling my greasemonkey functions 如何使用Greasemonkey阻止删除或停止jquery中的匿名函数 - How can I block remove or stop anonymous functions in jquery with Greasemonkey 如何改善我的“忽略列表” Greasemonkey脚本? - How can I improve my 'ignore-list' Greasemonkey script? 如何从Greasemonkey脚本中删除此CSS规则? - How can I remove this CSS rule from within a Greasemonkey script? 如何从Greasemonkey脚本拦截XMLHttpRequests? - How can I intercept XMLHttpRequests from a Greasemonkey script? 无法在从jquery脚本添加的表单上运行jquery函数 - Can't run jquery functions on forms added from jquery script 在Greasemonkey / javascript中,如何处理添加到页面的新元素? - In Greasemonkey/javascript, how can I handle new elements added to page? Greasemonkey函数在我的脚本中不起作用? - Greasemonkey functions don't work in my script? 我的脚本将在控制台中运行,但我无法让它在 Greasemonkey 中运行 - My script will run in the console, but I can not get it to run in Greasemonkey 如何在我的greasemonkey脚本中提供一个链接来备份GM _---值数据库? - How can I provide a link inside my greasemonkey script to backup the GM_---value database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM