0
  1. google.com/webstore i have add my extension
  2. i Have check "This item uses inline install."
  3. Websites: chose Verify site

  4. google.com/webmasters i have add site and Verifyed.

when i put this code on me site:

<link rel="chrome-webstore-item"href="https://chrome.google.com/webstore/detail/itemID">

<button onclick="chrome.webstore.install()" id="install-button">Add to Chrome</button>
<script>
if (document.getElementById('extension-is-installed')) {
  document.getElementById('install-button').style.display = 'none';
}
</script>

i click on button "Add to Chrome" install app extension, but when i refresh site button "Add to Chrome" is display. why? i cant Understanding

web404
  • 33
  • 1
  • 7

1 Answers1

1

You're obviously following the guide at https://developer.chrome.com/webstore/inline_installation

In that case, you missed a step.. Let's look at the code.

if (document.getElementById('extension-is-installed')) {
  document.getElementById('install-button').style.display = 'none';
}

The condition here is whether an element with ID extension-is-installed is present on the page. But what adds it?

A step back:

For example, you could have a content script that targets the installation page:

var isInstalledNode = document.createElement('div');
isInstalledNode.id = 'extension-is-installed';
document.body.appendChild(isInstalledNode);

So, you need to add a Content Script that adds that element to the page.


However, I doubt that guide will work. By default, content scripts execute after DOM is loaded (and therefore, that hiding script has executed). You can make them run at document_start, but then body does not exist yet.

Let me make an alternative hiding script, based on communicating with the extension using "externally_connectable". Suppose your website is example.com, and your extension's ID is itemID

  1. Add example.com to sites you want to be messaged from:

      "externally_connectable" : {
        "matches" : [
          "*://*.example.com/*"
        ]
      },
    
  2. In your background page, prepare for the message from the webpage:

    chrome.runtime.onMessageExternal.addListener(
      function(message, sender, sendResponse) {
        if(message.areYouThere) sendResponse(true);
      }
    );
    
  3. In your page at example.com, add a button (hidden by default) and code to show it when appropriate:

    <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>

Was requested to add page reload after install. chrome.webstore.install has a callback parameter specifically for this.

Instead of using onclick attribute, assign a function:

document.getElementById('install-button').addEventListener("click", function(e) {
  chrome.webstore.install(function() {
    // Installation successful
    location.reload();
  });
});
Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • it is working an thx you. one question , when i Click "Add to chrome" then click "Add" if i add extension i what to reload page . were can i put this code : location.reload(); – web404 Jan 21 '15 at 18:47
  • can u help me add , page reload – web404 Jan 22 '15 at 11:42
  • @web404 Edited the answer, but in future please consider asking follow-up questions separately. Take time to look at the site [tour]. – Xan Jan 22 '15 at 11:56
  • i cant understate were i pas put reload code. for this question i must create questions separately? or you can answer here? i didn't know English well, And excuse me if I did something wrong :( – web404 Jan 22 '15 at 15:34