在chromium源码中增加默认的搜索引擎
在chromium源码中增加默认的搜索引擎
接下的文章会涉及对chromium源代码的修改,会从一些简单的方向修改chromium的功能。本篇文章介绍如何在chromium中增加一个默认的搜索引擎。
以增加我们国家的搜索引擎–中国搜索 为例
1、首先需要定义一下搜索引擎
在src/chrome/browser/search_engines/prepopulated_engines.json文件中,定义了许多默认的搜索引擎,我们在该文件中,搜索 ,参考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
重新编译后就能在设置->搜索中看到我们添加的默认搜索引擎。