I'm creating a userscript that will inject a search box into this forum. The forum is running IP.Board, which includes a definition for $
in the javascript code.
This is the userscript code that I'm using:
// ==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;
});
However, when I use this user script, I get an error from the site saying TypeError: $(...) is null
, relating to the site's javascript (it's trying to reference the original definition of $
.
If I type $
into the dev console, I get the site's definition of it, and if I delete the text that is appended (everything in the .append(...)
), it works fine. The issue only occurs when I inject this text (or potentially something of similar length - I haven't tested that).
If I enter the code into the console, it functions fine, and the definition of $
doesn't change.