Commit d7063ecb authored by Nick Stenning's avatar Nick Stenning

Merge pull request #3097 from hypothesis/291-detect-existing-h-client

Enable Chrome extension to detect when H is already present on page
parents 229616ce 1bfb930a
function hypothesisConfig() {
// Pages on our site can include a meta tag to trigger specific behaviour
// when the extension loads.
var hypothesisIntent = document.querySelector('[name="hypothesis-intent"]');
return {
firstRun: hypothesisIntent && hypothesisIntent.content === 'first-run',
};
}
if (window.annotator) window.annotator.destroy();
delete window.annotator;
......@@ -33,8 +33,10 @@ Annotator.Plugin.CrossFrame.Bridge = require('../bridge');
Annotator.Plugin.CrossFrame.Discovery = require('../discovery');
var docs = 'https://h.readthedocs.org/en/latest/hacking/customized-embedding.html';
var appLinkEl =
document.querySelector('link[type="application/annotator+html"]');
var options = {
app: jQuery('link[type="application/annotator+html"]').attr('href')
app: appLinkEl.href,
};
if (window.hasOwnProperty('hypothesisConfig')) {
......@@ -54,5 +56,11 @@ Annotator.noConflict().$.noConflict(true)(function() {
Klass = options.constructor;
delete options.constructor;
}
window.annotator = new Klass(document.body, options);
appLinkEl.addEventListener('destroy', function () {
appLinkEl.parentElement.removeChild(appLinkEl);
window.annotator.destroy();
window.annotator = undefined;
});
});
'use strict';
/**
* Takes a Promise<T> and returns a Promise<Result>
* where Result = { result: T } | { error: any }.
......@@ -9,7 +11,7 @@ function toResult(promise) {
return promise.then(function (result) {
return { result: result };
}).catch(function (err) {
return { error: err }
return { error: err };
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment