1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-15 00:03:28 +01:00

client js: use iframe onload event to detect when iframe changes, allows

setting banner even for non-html captures, instead of frame notifying parent
will fix issue mentioned in #41
move script from frame_insert.html -> wb_frame.js
This commit is contained in:
Ilya Kreymer 2014-08-04 17:54:33 -07:00
parent 95028ab692
commit 25fe5d685c
6 changed files with 178 additions and 136 deletions

View File

@ -172,10 +172,12 @@ class LiveRewriter(object):
urlkey=urlkey,
cdx=cdx))
if env:
env['pywb.cdx'] = cdx
return result
def get_rewritten(self, *args, **kwargs):
result = self.fetch_request(*args, **kwargs)
status_headers, gen, is_rewritten = result

View File

@ -99,49 +99,15 @@ function remove_event(name, func, object) {
}
}
function notify_top(event) {
if (window.self == window.top) {
return;
}
if (window.top.top != window.top) {
return;
}
if (!window.WB_wombat_location) {
return;
}
if (wbinfo.is_embed) {
return;
}
if (event.target != window.document) {
return;
}
if (typeof(window.WB_wombat_location.href) != "string") {
return;
}
if (window.top.update_wb_url) {
window.top.update_wb_url(window.WB_wombat_location.href,
wbinfo.timestamp,
wbinfo.capture_str,
wbinfo.is_live);
}
}
var detect_on_init = function(event) {
init_banner();
notify_top(event);
init_banner();
remove_event("readystatechange", detect_on_init, document);
}
add_event("readystatechange", detect_on_init, document);
if (wbinfo.is_frame_mp && wbinfo.canon_url &&
(window.self == window.top) && (window.self.top == window.top) &&
window.location.href != wbinfo.canon_url) {
@ -149,6 +115,8 @@ if (wbinfo.is_frame_mp && wbinfo.canon_url &&
window.location.replace(wbinfo.canon_url);
}
return {'labels': labels};
return {'labels': labels,
'add_event': add_event,
'remove_event': remove_event};
})();

163
pywb/static/wb_frame.js Normal file
View File

@ -0,0 +1,163 @@
var update_wb_url = push_state;
function make_outer_url(url, ts)
{
if (ts) {
return wbinfo.prefix + ts + "/" + url;
} else {
return wbinfo.prefix + url;
}
}
function make_inner_url(url, ts)
{
if (ts) {
return wbinfo.prefix + ts + "mp_/" + url;
} else {
return wbinfo.prefix + "mp_/" + url;
}
}
function push_state(url, timestamp, capture_str, is_live) {
/* var curr_href = null;
if (window.frames[0].WB_wombat_location) {
curr_href = window.frames[0].WB_wombat_location.href;
}
if (url != curr_href) {
update_status(capture_str, is_live);
return;
}
if (!timestamp) {
timestamp = extract_ts(window.frames[0].location.href);
}
*/
var state = {}
state.timestamp = timestamp;
state.outer_url = make_outer_url(url, state.timestamp);
state.inner_url = make_inner_url(url, state.timestamp);
state.url = url;
state.capture_str = capture_str;
state.is_live = is_live;
window.history.replaceState(state, "", state.outer_url);
update_status(state.capture_str, is_live);
}
function pop_state(state) {
update_status(state.capture_str, state.is_live);
window.frames[0].src = state.outer_url;
}
function extract_ts(url)
{
var inx = url.indexOf("mp_");
if (inx < 0) {
return "";
}
url = url.substring(0, inx);
inx = url.lastIndexOf("/");
if (inx <= 0) {
return "";
}
return url.substring(inx + 1);
}
function extract_replay_url(url) {
var inx = url.indexOf("/http:");
if (inx < 0) {
inx = url.indexOf("/https:");
if (inx < 0) {
return "";
}
}
return url.substring(inx + 1);
}
function update_status(str, is_live) {
var capture_info = document.getElementById("_wb_capture_info");
if (capture_info) {
capture_info.innerHTML = str;
}
var label = document.getElementById("_wb_label");
if (label) {
if (is_live) {
label.innerHTML = _wb_js.labels.LIVE_MSG;
} else {
label.innerHTML = _wb_js.labels.REPLAY_MSG;
}
}
}
function ts_to_date(ts, is_gmt)
{
if (ts.length < 14) {
return ts;
}
var datestr = (ts.substring(0, 4) + "-" +
ts.substring(4, 6) + "-" +
ts.substring(6, 8) + "T" +
ts.substring(8, 10) + ":" +
ts.substring(10, 12) + ":" +
ts.substring(12, 14) + "-00:00");
var date = new Date(datestr);
if (is_gmt) {
return date.toGMTString();
} else {
return date.toLocaleString();
}
}
window.onpopstate = function(event) {
var curr_state = event.state;
if (curr_state) {
pop_state(curr_state);
}
}
function extract_ts_cookie(value) {
var regex = /pywb.timestamp=([\d]{1,14})/;
var result = value.match(regex);
if (result) {
return result[1];
} else {
return "";
}
}
function iframe_loaded(event) {
var iframe = window.frames[0];
var url;
var ts;
var capture_str;
var is_live = false;
if (iframe.WB_wombat_location) {
url = window.WB_wombat_location.href;
} else {
url = extract_replay_url(iframe.location.href);
}
if (iframe.wbinfo) {
ts = iframe.wbinfo.timestamp;
is_live = iframe.wbinfo.is_live;
capture_str = iframe.wbinfo.capture_str;
} else {
ts = extract_ts(iframe.location.href);
if (!ts) {
is_live = true;
ts = extract_ts_cookie(iframe.document.cookie);
}
capture_str = ts_to_date(ts, true);
}
update_wb_url(url, ts, capture_str, is_live);
}

View File

@ -10,107 +10,12 @@
wbinfo.is_frame = true;
</script>
<script src='{{ wbrequest.host_prefix }}/{{ static_path }}/wb.js'> </script>
<script>
var update_wb_url = push_state;
function make_outer_url(url, ts)
{
if (ts) {
return wbinfo.prefix + ts + "/" + url;
} else {
return wbinfo.prefix + url;
}
}
function make_inner_url(url, ts)
{
if (ts) {
return wbinfo.prefix + ts + "mp_/" + url;
} else {
return wbinfo.prefix + "mp_/" + url;
}
}
function push_state(url, timestamp, capture_str, is_live) {
var curr_href = null;
if (window.frames[0].WB_wombat_location) {
curr_href = window.frames[0].WB_wombat_location.href;
}
if (url != curr_href) {
update_status(capture_str, is_live);
return;
}
if (!timestamp) {
timestamp = extract_ts(window.frames[0].location.href);
}
var state = {}
state.timestamp = timestamp;
state.outer_url = make_outer_url(url, state.timestamp);
state.inner_url = make_inner_url(url, state.timestamp);
state.url = url;
state.capture_str = capture_str;
state.is_live = is_live;
window.history.replaceState(state, "", state.outer_url);
update_status(state.capture_str, is_live);
}
function pop_state(state) {
update_status(state.capture_str, state.is_live);
window.frames[0].src = state.outer_url;
}
function extract_ts(url)
{
var inx = url.indexOf("mp_");
if (inx < 0) {
return "";
}
url = url.substring(0, inx);
inx = url.lastIndexOf("/");
if (inx <= 0) {
return "";
}
return url.substring(inx + 1);
}
function update_status(str, is_live) {
var capture_info = document.getElementById("_wb_capture_info");
if (capture_info) {
capture_info.innerHTML = str;
}
var label = document.getElementById("_wb_label");
if (label) {
if (is_live) {
label.innerHTML = _wb_js.labels.LIVE_MSG;
} else {
label.innerHTML = _wb_js.labels.REPLAY_MSG;
}
}
}
window.onpopstate = function(event) {
var curr_state = event.state;
if (curr_state) {
pop_state(curr_state);
}
}
</script>
<script src='{{ wbrequest.host_prefix }}/{{ static_path }}/wb_frame.js'> </script>
<link rel='stylesheet' href='{{ wbrequest.host_prefix }}/{{ static_path }}/wb.css'/>
<!-- End WB Insert -->
<body style="margin: 0px; padding: 0px;">
<div class="wb_iframe_div">
<iframe src="{{ wbrequest.wb_prefix + embed_url }}" seamless="seamless" frameborder="0" scrolling="yes" class="wb_iframe"/>
<iframe src="{{ wbrequest.wb_prefix + embed_url }}" onload="iframe_loaded(event);" seamless="seamless" frameborder="0" scrolling="yes" class="wb_iframe"/>
</div>
</body>
</html>

View File

@ -57,6 +57,10 @@ class RewriteHandler(SearchPageWbUrlHandler):
return self._make_response(wbrequest, *result)
def _make_response(self, wbrequest, status_headers, gen, is_rewritten):
cdx = wbrequest.env['pywb.cdx']
cookie = 'pywb.timestamp=' + cdx['timestamp'] + '; max-age=60'
status_headers.headers.append(('Set-Cookie', cookie))
return WbResponse(status_headers, gen)
def __str__(self):

View File

@ -41,7 +41,7 @@ class template_filter(object):
@template_filter
def format_ts(value, format_='%a, %b %d %Y %H:%M:%S'):
value = timestamp_to_datetime(value)
return value.strftime(format_)
return value.strftime(format_) + ' GMT'
@template_filter('urlsplit')