Omnibox API |打开特定关键字的特定窗口

时间:2011-05-15 15:09:20

标签: html google-chrome google-chrome-extension

我正在尝试创建多功能框快捷方式,因此当用户键入cp commandcp command 2时,它会打开窗口1或窗口2,但它会打开“cp”上的两个窗口或“cp {any here}”。

我是否遗漏了API的内容?

background.html

<script>
chrome.omnibox.onInputChanged.addListener(
  function sharePage(tweet, suggest) {
    suggest([
      {content: "tweet", description: "Share on Twitter"}
    ]);
  });
//
chrome.omnibox.onInputEntered.addListener(
  function sharePage(tweet) {
      chrome.tabs.getSelected(null, function (tab) {
      var url = "https://twitter.com/home?status=Check%20out%20" + encodeURIComponent(tab.url) + "%20via @Chromeplete"
    chrome.tabs.create ({"url": url});
  });
 });
</script>
<script>
chrome.omnibox.onInputChanged.addListener(
  function sharePage(post, suggest) {
    suggest([
      {content: "post", description: "Share on Facebook"}
    ]);
  });
//
chrome.omnibox.onInputEntered.addListener(
  function sharePage(post) {
      chrome.tabs.getSelected(null, function (tab) {
      var url = "https://www.facebook.com/sharer.php?u" + encodeURIComponent(tab.url) + "&appid=127651283979691"
    chrome.tabs.create ({"url": url});
  });
 });
</script>

1 个答案:

答案 0 :(得分:1)

应该是这样的:

chrome.omnibox.onInputChanged.addListener(function(text, suggest) {
    suggest([
      {content: "tweet", description: "Share on Twitter"},
      {content: "post", description: "Share on Facebook"}
    ]);
});
//
chrome.omnibox.onInputEntered.addListener(function(text) {
      chrome.tabs.getSelected(null, function (tab) {
          if(text == "tweet") {
              var url = "https://twitter.com/home?status=Check%20out%20" + encodeURIComponent(tab.url) + "%20via @Chromeplete";
              chrome.tabs.create ({"url": url});
          } else if(text == "post") {
              var url = "https://www.facebook.com/sharer.php?u" + encodeURIComponent(tab.url) + "&appid=127651283979691";
              chrome.tabs.create ({"url": url});
          }

      });
});