Created
May 22, 2017 17:00
-
-
Save judell/c93515819d963bf19b4b294aa3609053 to your computer and use it in GitHub Desktop.
h_share
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://h.jonudell.info/h_share?id=sXl8jPYkEeaFkovibrSZ-g | |
@view_config( route_name='h_share' ) | |
def h_share(request): | |
q = urlparse.parse_qs(request.query_string) | |
id = q['id'][0] | |
logger.info('id %s' % id) | |
row = requests.get('https://hypothes.is/api/annotations/' + id).json() | |
anno = HypothesisAnnotation(row) | |
uri = anno.uri | |
quote = anno.exact | |
if quote is None: | |
quote = '' | |
comment = anno.text | |
if comment is None: | |
comment = '' | |
logger.info('quote %s' % quote) | |
logger.info('comment %s' % comment) | |
page = """ | |
<!doctype html> | |
<html lang=en> | |
<head> | |
<meta charset=utf-8> | |
<meta name="twitter:card" content="summary" /> | |
<meta name="twitter:site" content="@hypothes_is" /> | |
<meta name="twitter:title" content="__QUOTE__" /> | |
<meta name="twitter:description" content="__COMMENT__" /> | |
<meta name="twitter:image" content="http://hyp.is/static/images/twitter.png" /> | |
<meta property="og:type" content="article"/> | |
<meta property="og:site_name" content="Hypothes.is"/> | |
<meta property="og:title" content="__QUOTE__"/> | |
<meta property="og:description" content="__COMMENT__"/> | |
<meta property="og:image" content="http://hyp.is/static/images/twitter.png"/> | |
<title>Hypothesis: Annotation for __URI__</title> | |
<link rel="stylesheet" | |
type="text/css" | |
href="https://hyp.is/static/styles/bouncer.css"> | |
</head> | |
<body> | |
<div class="center"> | |
<div class="center spinner__stationary-ring"> | |
<img alt="Loading annotation for en.wikipedia.org" | |
class="center spinner__icon" | |
height="24" | |
src="/static/images/hypothesis-icon.svg" | |
width="28"> | |
<div class="spinner__moving-ring"></div> | |
</div> | |
<p class="spinner__text">Loading annotation for __URI__</p> | |
</div> | |
</body> | |
<!-- Render a configuration object for the redirect.js to pick up. --> | |
<script class="js-bouncer-settings" type="application/json"> | |
{"viaUrl": "https://via.hypothes.is/__URI__#annotations:__ID__", "extensionUrl": "__URI__#annotations:__ID__", "chromeExtensionId": "bjfhmglciegochdpefhhlphglcehbmek"} | |
</script> | |
<script> | |
'use strict'; | |
/** Return the settings object that the server injected into the page. */ | |
function getSettings(document) { | |
return JSON.parse( | |
document.querySelector('script.js-bouncer-settings').textContent); | |
} | |
/** Navigate the browser to the given URL. */ | |
function navigateTo(url) { | |
window.location.replace(url); | |
} | |
/** Navigate the browser to the requested annotation. | |
* | |
* If the browser is Chrome and our Chrome extension is installed then | |
* navigate to the annotation's direct link for the Chrome extension. | |
* If the Chrome extension isn't installed or the browser isn't Chrome then | |
* navigate to the annotation's Via direct link. | |
* | |
*/ | |
function redirect(navigateToFn) { | |
// Use the test navigateTo) function if one was passed in, the real | |
// navigateTo() otherwise. | |
navigateTo = navigateToFn || navigateTo; | |
var settings = getSettings(document); | |
if (window.chrome && chrome.runtime && chrome.runtime.sendMessage) { | |
// The user is using Chrome, redirect them to our Chrome extension if they | |
// have it installed, via otherwise. | |
chrome.runtime.sendMessage( | |
settings.chromeExtensionId, | |
{type: 'ping'}, | |
function (response) { | |
var url; | |
if (response && !chrome.runtime.lastError) { | |
// The user has our Chrome extension installed :) | |
url = settings.extensionUrl; | |
} else { | |
if (chrome.runtime.lastError) { | |
console.error(chrome.runtime.lastError); | |
} | |
// The user doesn't have our Chrome extension installed :( | |
url = settings.viaUrl; | |
} | |
navigateTo(url); | |
} | |
); | |
} else { | |
// The user isn't using Chrome, just redirect them to Via. | |
navigateTo(settings.viaUrl); | |
} | |
} | |
if (typeof module === 'object') { | |
// Browserify is present, this file must be being run by the tests. | |
module.exports = redirect; | |
} else { | |
// Browserify is not present, this file must be being run in development or | |
// production. | |
redirect(); | |
} | |
</script> | |
</html> | |
""" | |
page = page.replace('__QUOTE__', quote) | |
page = page.replace('__COMMENT__', comment) | |
page = page.replace('__ID__', id) | |
page = page.replace('__URI__', uri) | |
r = Response(page) | |
r.content_type = 'text/html' | |
return r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment