|
var CONTENT = { |
|
'1': 'This is the content for the first chapter', |
|
'2': 'Content for the second chapter', |
|
'3': 'Content for the third chapter', |
|
}; |
|
|
|
function reloadHypothesis() { |
|
var H_SERVER = 'https://hypothes.is'; |
|
|
|
// Remove existing Hypothesis instance (if any) |
|
var links = [].slice.apply(document.querySelectorAll('link')); |
|
links.forEach(function (linkEl) { |
|
if (linkEl.type === 'application/annotator+html' || |
|
linkEl.href.startsWith(H_SERVER)) { |
|
linkEl.remove(); |
|
} |
|
}); |
|
var scripts = [].slice.apply(document.querySelectorAll('script')); |
|
scripts.forEach(function (scriptEl) { |
|
if (scriptEl.src.startsWith(H_SERVER)) { |
|
scriptEl.remove(); |
|
} |
|
}); |
|
|
|
if (window.annotator) { |
|
// Remove event listeners for existing Hypothesis |
|
// instance. This works around a bug in Hypothesis <= 0.21.0 |
|
// where window.annotator.destroy() fails to do this itself. |
|
var jq = require('jquery'); |
|
var events = ['beforeAnnotationCreated', |
|
'annotationCreated', |
|
'annotationUpdated', |
|
'annotationsLoaded', |
|
'annotationsUnloaded', |
|
'annotationDeleted', |
|
'panelReady', |
|
'setVisibleHighlights']; |
|
events.forEach(function (event) { |
|
jq(document.body).unbind(event); |
|
}); |
|
|
|
// Remove the Hypothesis UI from the page |
|
window.annotator.destroy(); |
|
} |
|
|
|
// Install Hypothesis for current URL |
|
var embedScriptEl = document.createElement('script'); |
|
embedScriptEl.src = H_SERVER + '/embed.js'; |
|
document.head.appendChild(embedScriptEl); |
|
} |
|
|
|
var contentEl = document.querySelector('.js-content'); |
|
|
|
page('/demo.html', function () { |
|
contentEl.innerHTML = 'Click the chapter links on the left'; |
|
reloadHypothesis(); |
|
}); |
|
|
|
page('/chapters/:id', function (context) { |
|
contentEl.innerHTML = CONTENT[context.params.id]; |
|
reloadHypothesis(); |
|
}); |
|
|
|
document.addEventListener('DOMContentLoaded', function () { |
|
page.start(); |
|
}); |
This comment has been minimized.
@robertknight is this still the recommended approach to deal with the issue?