1. // omnibox 演示
    2. chrome.omnibox.onInputChanged.addListener((text, suggest) => {
    3. console.log('inputChanged: ' + text);
    4. if(!text) return;
    5. if(text == '美女') {
    6. suggest([
    7. {content: '中国' + text, description: '你要找“中国美女”吗?'},
    8. {content: '日本' + text, description: '你要找“日本美女”吗?'},
    9. {content: '泰国' + text, description: '你要找“泰国美女或人妖”吗?'},
    10. {content: '韩国' + text, description: '你要找“韩国美女”吗?'}
    11. ]);
    12. }
    13. else if(text == '微博') {
    14. suggest([
    15. {content: '新浪' + text, description: '新浪' + text},
    16. {content: '腾讯' + text, description: '腾讯' + text},
    17. }
    18. else {
    19. suggest([
    20. {content: '百度搜索 ' + text, description: '百度搜索 ' + text},
    21. {content: '谷歌搜索 ' + text, description: '谷歌搜索 ' + text},
    22. ]);
    23. }
    24. });
    25. // 当用户接收关键字建议时触发
    26. chrome.omnibox.onInputEntered.addListener((text) => {
    27. console.log('inputEntered: ' + text);
    28. if(!text) return;
    29. var href = '';
    30. if(text.endsWith('美女')) href = 'http://image.baidu.com/search/index?tn=baiduimage&ie=utf-8&word=' + text;
    31. else if(text.startsWith('百度搜索')) href = 'https://www.baidu.com/s?ie=UTF-8&wd=' + text.replace('百度搜索 ', '');
    32. else if(text.startsWith('谷歌搜索')) href = 'https://www.google.com.tw/search?q=' + text.replace('谷歌搜索 ', '');
    33. openUrlCurrentTab(href);
    34. });
    35. // 获取当前选项卡ID
    36. function getCurrentTabId(callback)
    37. {
    38. chrome.tabs.query({active: true, currentWindow: true}, function(tabs)
    39. {
    40. if(callback) callback(tabs.length ? tabs[0].id: null);
    41. });
    42. }
    43. // 当前标签打开某个链接
    44. function openUrlCurrentTab(url)
    45. {
    46. getCurrentTabId(tabId => {
    47. chrome.tabs.update(tabId, {url: url});
    48. })