Skip to content

Commit 63ebf1a

Browse files
committed
allow enabling hypothesis by sending a message
* The child frame now listens for `startHypothesis` events. When received, it will inject the hypothesis client loader into the page and apply a class to the page body to move pdf.js action buttons to accomodate the H sidebar. The start code will only be run once, subsequent attempts will do nothing. If the parent frame has loaded mfr.js, it can send the event by calling the `startHypothesis` method on the renderer class. This will relay the event through pym.js. If the parent frame has not loaded mfr.js, it may send the event via: `childIframe.contentWindow.postMessage('startHypothesis', parentDomain)` This feature must be enabled in MFR by setting the `PDF_EXTENSION_CONFIG_ENABLE_HYPOTHESIS` envvar to 1.
1 parent 6690400 commit 63ebf1a

File tree

3 files changed

+36
-7
lines changed

3 files changed

+36
-7
lines changed

mfr/extensions/pdf/templates/viewer.mako

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,16 @@ http://sourceforge.net/adobe/cmap/wiki/License/
3737

3838
<script src="debugger.js"></script>
3939
<script src="viewer.js"></script>
40+
4041
% if enable_hypothesis:
41-
<script src="https://hypothes.is/embed.js"></script>
42-
<style>
43-
#toolbarViewer {
44-
position: relative;
45-
margin-right: 36px;
46-
}
47-
</style>
42+
<style>
43+
body.show-hypothesis #toolbarViewer {
44+
position: relative;
45+
margin-right: 36px;
46+
}
47+
</style>
4848
% endif
49+
4950
</head>
5051

5152
<body tabindex="1" class="loadingInProgress">

mfr/server/static/js/mfr.child.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
;(function() {
22
'use strict';
3+
34
window.pymChild = new pym.Child();
45

56
window.addEventListener('load', function () {
@@ -25,4 +26,27 @@
2526
window.pymChild.onMessage('resize', function () {
2627
window.pymChild.sendHeight();
2728
});
29+
30+
var hypothesisLoaded = false;
31+
32+
window.pymChild.onMessage('startHypothesis', startHypothesis);
33+
34+
window.addEventListener('message', function(event) {
35+
if (event.data === 'startHypothesis') {
36+
startHypothesis(event);
37+
}
38+
});
39+
40+
function startHypothesis(event) {
41+
if (hypothesisLoaded) {
42+
return;
43+
}
44+
45+
var script = window.document.createElement('script');
46+
script.type = 'text/javascript';
47+
script.src = 'https://hypothes.is/embed.js';
48+
window.document.head.appendChild(script);
49+
window.document.body.classList.add('show-hypothesis');
50+
hypothesisLoaded = true;
51+
};
2852
})();

mfr/server/static/js/mfr.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@
111111
self.pymParent.sendMessage('resize', 'x');
112112
};
113113

114+
self.startHypothesis = function() {
115+
self.pymParent.sendMessage('startHypothesis');
116+
};
117+
114118
return self;
115119
};
116120

0 commit comments

Comments
 (0)