jQuery noConflict无法正常工作

时间:2014-12-17 22:34:53

标签: javascript jquery greasemonkey userscripts

我正在创建一个将搜索框注入this forum的用户脚本。该论坛正在运行IP.Board,其中包含javascript代码中$的定义。 这是我使用的用户代码:

// ==UserScript==
// @name LTT Search redirector
// @namespace   1fb3e69b2f9d1a868b4e5a4c8c559152
// @description Replaces the target of the search box on linustechtips.com with a google search within linustechtips.com. the URL used is https://www.google.com/search?q=site:linustechtips.com+%s
// @include /^https?:\/\/(?:[^\.]+\.)?linustechtips\.com(?:\/.*)*$/
// @require https://code.jquery.com/jquery-2.1.1.min.js
// @version 2.0.0
// @updateURL   https://monkeyguts.com/260.meta.js?c
// @downloadURL https://monkeyguts.com/260.user.js?c
// ==/UserScript==

var $jq = jQuery.noConflict(true);

if($jq("#search-box").length === 0) { //Search box does not exist
    $jq("#branding .main_width").append('<div id="search" class="right">'+
'   <form action="http://linustechtips.com/main/" method="post" id="search-box">'+
'           <span class="right" id="search_wrap">'+
'               <input type="text" tabindex="100" size="17" class="" name="search_term" id="main_search" placeholder="Search...">'+
'               <span style="" id="search_options" class="choice ipbmenu">Using Google</span>'+
'               <input type="submit" value="" class="submit_input clickable">'+
'           </span>'+
'   </form>'+
'</div>');
}

$jq("#search-box .submit_input").click(function(){
    window.location.href="https://www.google.com/search?q=site:linustechtips.com+"+
        (jQuery("#main_search").val().replace(/(\s)/g,"+"));
    return false;
});

但是,当我使用此用户脚本时,我收到来自网站的TypeError: $(...) is null错误,该错误与网站的javascript有关(它试图引用{{的原始定义) 1}}。

如果我在开发控制台中键入$,我会得到网站的定义,如果我删除了附加的文本($中的所有内容),它就有效精细。这个问题只发生在我注入这个文本时(或者可能是类似长度的东西 - 我还没有对它进行过测试)。

如果我将代码输入控制台,它运行正常,.append(...)的定义不会改变。

0 个答案:

没有答案