• Robert Knight's avatar
    Enable Chrome extension to detect when H is already present on page · 1bfb930a
    Robert Knight authored
    This reworks the way that Hypothesis detects whether it is
    already present on the page in embed.js, in a way that
    enables it to report back the app.html URL for the
    existing instance to the Chrome extension.
    
    The Chrome extension can then update its state for that
    tab to reflect the fact that it is not injected.
    
    In this PR, that is done by simply setting the tab state
    to inactive. We could in future add an indicator that
    the user tried to activate H but that another instance
    is already active, or have the extension override the
    instance of H on the page.
    
     * Detect the <link> element added to the page by embed.js,
       rather than window.annotator. This enables detection to work
       when embed.js is run in an isolated world that shares the DOM
       but not the JS environment, as is the case for content scripts
       in Chrome and Firefox.
    
     * Change unloading to operate by firing an event at the <link>
       element. This enables a content script to trigger unloading
       of H from an isolated JS environment.
    
     * Change injection of embed.js in the Chrome extension to
       execute embed.js as a content script, rather than by
       adding it as a `<script>` tag. This enables embed.js to
       access extension APIs and also report its result back
       to the extension via the return value of the script.
    
       A side benefit is that it also avoids a bug in Firefox
       current WebExtensions implementation where `<script>` tags
       added to the page by a content script are not executed.
    1bfb930a
promise-util.js 420 Bytes