接下的文章會涉及對chromium源代碼的修改,會從一些簡單的方向修改chromium的功能。本篇文章介紹如何在chromium中增加一個默認的搜索引擎。
以增加我們國家的搜索引擎--中國搜索 http://www.chinaso.com/ 爲例
1、首先需要定義一下搜索引擎
在src/chrome/browser/search_engines/prepopulated_engines.json文件中,定義了許多默認的搜索引擎,我們在該文件中,搜索www.sogou.com,參考sogou的定義,在該文件中增加一條chinaso的配置。
name:“中國搜索”的utf8編碼。
keyword:上圖配置中第二列關鍵字。
favicon_url:網站圖標,通常都會有,就在網站後邊加個favicon.ico
search_url:這個是關鍵,需要根據搜索站點的url格式,拼出一個搜索的url,當用戶在地址欄輸入query後,會用query替換{searchTerms}
type:搜索引擎的type,按照例子造了一個。
id:prepopulated_engines.json文件上邊有說明,“The following unique IDs are available:11, 12, 14, 18, 19, 20..... “,隨便選一個。
"yandex_ua": {
"name": "\u042f\u043d\u0434\u0435\u043a\u0441",
"keyword": "yandex.ua",
"favicon_url": "http://yandex.ua/favicon.ico",
"search_url": "http://yandex.ua/yandsearch?text={searchTerms}",
"suggest_url": "http://suggest.yandex.net/suggest-ff.cgi?part={searchTerms}",
"type": "SEARCH_ENGINE_YANDEX",
"id": 15
},
"chinaso": {
"name": "\u4e2d\u56fd\u641c\u7d22",
"keyword": "chinaso.com",
"favicon_url": "http://www.chinaso.com/favicon.ico",
"search_url": "http://www.chinaso.com/search/pagesearch.htm?q={searchTerms}",
"type": "SEARCH_ENGINE_CHINASO",
"id": 11
},
// UMA-only engines ////////////////////////////////////////////////////////
// The following engines are not included in any of the country lists. They
// are listed in |kAllEngines|, however, so that GetEngineType can find them
// for UMA reporting purposes.
2、配置中文默認搜索引擎。
在src\chrome\browser\search_engines\template_url_prepopulate_data.cc中修改
在engines_CN中加入“chinaso“,就是prepopulated_engines.json定義的搜索引擎
// China
const PrepopulatedEngine* engines_CN[] =
{ &chinaso, &baidu, &sogou, &soso, &google };
在kAllEngines中也加入“chinaso“,這個在GetEngineType函數中會用到。
// A list of all the engines that we know about.
const PrepopulatedEngine* kAllEngines[] = {
......
&yahoo_vn, &yahoo_za, &yandex_ru, &yandex_tr, &yandex_ua,
&chinaso,
// UMA-only engines:
......
}
3、增加一個搜索引擎類型
src/chrome/browser/search_engines/search_engine_type.h
在SearchEngineType的SEARCH_ENGINE_MAX之前,增加一項SEARCH_ENGINE_CHINASO
重新編譯後就能在設置->搜索中看到我們添加的默認搜索引擎。