Tùy biến công cụ tìm kiếm google cho mybb - ngocvu1403 - 04-07-2013 Hum nay ngồi tổng hợp thêm 1 cái nữa cho mybb ...( phòng sau này quên)hihi   Demo: cái thanh search bên trên góc phải của diễn đàn nhé: Các bước thực hiện: 1. Tạo công cụ tìm kiếm tùy chỉnh - Custom Search Engine (CSE) - Truy cập vào Custom Search Engine và đăng nhập bằng tài khoản Google của mọi người. - Điền đầy đủ thông tin cần thiết và tiến hành cho đến khi lấy được code (Bước 3. Nhận mã). Chú ý ở chỗ "Trang web tìm kiếm:" thì nhập URL diễn đàn hoặc trang web của mọi người, mỗi URL trên 1 dòng. Ở bước này nếu ai gặp khó khăn thì có thể search thêm trên net rất nhiều.  Ví dụ mình nhận được mã sau: Code: <div id="cse" style="width: 100%;">Loading</div> <script src="http://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"> google.load('search', '1', {language : 'vi', style : google.loader.themes.V2_DEFAULT}); google.setOnLoadCallback(function() { var customSearchOptions = {}; var customSearchControl = new google.search.CustomSearchControl( '004142063128979054355:eczye4qariq', customSearchOptions); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); customSearchControl.draw('cse'); }, true); </script>
Mọi người lấy cái phần code sau: Code: 004142063128979054355:eczye4qariq
2. Tạo module tìm kiếm sử dụng Google Custom Search: - Vào AdminCP » Template Sets » Global Templates » Add Template >> Template Name: customsearch >> Nội dung của template: Trích dẫn:<html> <head> <title>{$mybb->settings['bbname']} - {$lang->toplinks_search}</title> {$headerinclude} </head> <body> {$header} <table cellspacing="0" cellpadding="4" border="0" class="tborder"> <tbody><tr> <td class="thead"><strong>{$mybb->settings['bbname']} - Tìm kiếm với Google</strong></td> </tr> <tr> <td class="trow1"> <form method="GET" action="customsearch.php"> <fieldset class="noborder"> <label for="search_box"> <span id="search_left"></span> </label> <input type="text" style="width: 85%;" title="Nhập từ khóa bạn muốn tìm kiếm" id="search_box" name="q" value="Tìm kiếm trong diễn đàn với Google..." onfocus="if(this.value == 'Tìm kiếm trong diễn đàn với Google...') { this.value = ''; }" onblur="if(this.value=='') { this.value='Tìm kiếm trong diễn đàn với Google...'; }"> <input type="submit" id="search_button" name="submit-search" value="Tìm kiếm"> </fieldset> </form> </td> </tr> <tr> <td class="trow1"> <div id="cse" style="width: 100%;"></div> <script src="http://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"> function parseQueryFromUrl () { var queryParamName = "q"; var search = window.location.search.substr(1); var parts = search.split('&'); for (var i = 0; i < parts.length; i++) { var keyvaluepair = parts[i].split('='); if (decodeURIComponent(keyvaluepair[0]) == queryParamName) { return decodeURIComponent(keyvaluepair[1].replace(/\+/g, ' ')); } } return ''; } google.load('search', '1'); google.setOnLoadCallback(function() { var customSearchControl = new google.search.CustomSearchControl('015027525266926634575:sg4ezpg7vgm'); customSearchControl.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET); var options = new google.search.DrawOptions(); options.setAutoComplete(true); options.enableSearchResultsOnly(); customSearchControl.draw('cse', options); var queryFromUrl = parseQueryFromUrl(); if (queryFromUrl) { customSearchControl.execute(queryFromUrl); } }, true); </script> <link rel="stylesheet" href="http://www.google.com/cse/style/look/default.css" type="text/css" /> </td> </tr> </tbody></table> {$footer} </body> </html> Ở nội dung trên, chúng ta chú ý dòng sau: Code: var customSearchControl = new google.search.CustomSearchControl('004142063128979054355:eczye4qariq');
Thay bằng code của mọi người có được ở bước trên vào đó. - Tạo file customsearch.php với nội dung sau: Code: <?php define("IN_MYBB",1); require_once("./global.php"); add_breadcrumb("Custom Search", "customsearch.php"); eval("\$customsearch = \"".$templates->get("customsearch")."\";"); output_page($customsearch); ?>
và upload lên host ngang hàng với index của diễn đàn. Bây giờ coi như đã thành tạo thành công module search của riêng mọi người sử dụng Google Custom Search. Mọi người có thể test thử theo link YourDomain/MyBB_ROOT/customsearch.php 3. Sử dụng: - Có thể chèn link trên ở đâu đó trong diễn đàn để thành viên sử dụng. - Có thể add Search Box vào banner như của mình: Sử dụng code để chèn vào template: Trích dẫn:<div id="search"> <form action="{$mybb->settings['bburl']}/customsearch.php" method="GET"> <fieldset class="noborder"> <label for="search_box"> <span id="search_left"></span> </label> <input name="q" id="search_box" onkeyup="buscar_ahora(this.value);" onblur="if(this.value=='')this.value='Tìm kiếm với google';" onclick="if(this.value=='Tìm kiếm với google')this.value='';" value="Tìm kiếm với google" title="Nhập từ khóa bạn muốn tìm kiếm" type="text" /> <input value="{$lang->toplinks_search}" name="submit-search" id="search_button" type="submit" /> </fieldset> </form> </div> css: (thêm vào global.css) Code: div#search { top: 55px; right: -5px; height: 26px; position: absolute; display: inline-table; float: right; }
Hoặc đâu đó mọi nơi mọi người muốn. Nguồn: mybb.vn |