如何在Google扩展程序后重新加载页面

时间:2015-01-23 15:28:27

标签: google-chrome-extension

我想在google扩展程序后重新加载页面。

有背景页面代码:

chrome.runtime.onMessageExternal.addListener(
  function(message, sender, sendResponse) {
    if(message.areYouThere) sendResponse(true);
  }
);

为网页添加按钮代码:

<button onclick="chrome.webstore.install()"
      id="install-button" style="display:none;">
      Add to Chrome
    </button>
    <script>
      if (chrome) {
        // The browser is Chrome, so we may need to show the button
        if(chrome.runtime && chrome.runtime.sendMessage) {
          // Some extension is ready to receive messages from us
          // Test it:
          chrome.runtime.sendMessage(
            "itemID",
            {areYouThere: true},
            function(response) {
              if(response) {
                // Extension is already installed, keep hidden
              } else {
                // No positive answer - it wasn't our extension
                document.getElementById('install-button').style.display = 'block';
              }
            }
          );
        } else {
          // Extension is not installed, show button
          document.getElementById('install-button').style.display = 'block';
        }
      }
    </script>

我希望在Google扩展程序后重新加载页面。 我必须把这段代码用来做这个功能

document.getElementById('install-button').addEventListener("click", function(e) {
  chrome.webstore.install(function() {
    // Installation successful
    location.reload();
  });
}); 

1 个答案:

答案 0 :(得分:1)

就在那里。

此代码from my answer可替代您的onclick属性。

<button id="install-button" style="display:none;">
  Add to Chrome
</button>
<script>
  // Add a click handler
  document.getElementById('install-button').addEventListener(/* ... */);

  // Show if necessary
  if (chrome) {
    /* ... */
  }
</script>

(为简洁而采取的代码)