Skip to content

Commit a61685c

Browse files
committed
wombat fixes: ensure document.open and document.write overrides correct
for rewrite_html, check if head, body tags were present and include in rewritten version for referrer-rel rewrite, use top origin
1 parent aaae4d7 commit a61685c

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

pywb/static/wombat.js

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,23 +223,27 @@ var wombat_internal = function($wbwindow) {
223223
var prefix = starts_with(url, VALID_PREFIXES);
224224

225225
if (prefix) {
226-
var prefix_host = prefix + $wbwindow.location.host + '/';
226+
var orig_host = $wbwindow.top.location.host;
227+
var orig_protocol = $wbwindow.top.location.protocol;
228+
229+
var prefix_host = prefix + orig_host + '/';
230+
227231
// if already rewritten url, must still check scheme
228232
if (starts_with(url, prefix_host)) {
229233
if (starts_with(url, wb_replay_prefix)) {
230234
return url;
231235
}
232236

233-
var curr_scheme = $wbwindow.location.protocol + '//';
234-
var host = $wbwindow.location.host + '/';
237+
var curr_scheme = orig_protocol + '//';
238+
var host = orig_host + '/';
235239
var path = url.substring(prefix_host.length);
236240
var rebuild = false;
237241

238242
if (path.indexOf(wb_coll_prefix_check) < 0 && url.indexOf("/static/") < 0) {
239243
path = wb_coll_prefix + WB_wombat_location.origin + "/" + path;
240244
rebuild = true;
241245
}
242-
246+
243247
// replace scheme to ensure using the correct server scheme
244248
//if (starts_with(url, wb_orig_scheme) && (wb_orig_scheme != curr_scheme)) {
245249
if (prefix != curr_scheme && prefix != REL_PREFIX) {
@@ -1057,6 +1061,8 @@ var wombat_internal = function($wbwindow) {
10571061
}
10581062

10591063
var new_html = "";
1064+
var head = "";
1065+
var body = "";
10601066

10611067
if (inner_doc.head.innerHTML) {
10621068
var elems = inner_doc.head.children;
@@ -1065,7 +1071,7 @@ var wombat_internal = function($wbwindow) {
10651071
// Call orig write to ensure same execution order and placement
10661072
rewrite_elem(elems[i]);
10671073
}
1068-
new_html += inner_doc.head.innerHTML;
1074+
head += inner_doc.head.innerHTML;
10691075
}
10701076

10711077
if (inner_doc.body.innerHTML) {
@@ -1075,7 +1081,23 @@ var wombat_internal = function($wbwindow) {
10751081
// Call orig write to ensure same execution order and placement
10761082
rewrite_elem(elems[i]);
10771083
}
1078-
new_html += inner_doc.body.innerHTML;
1084+
body += inner_doc.body.innerHTML;
1085+
}
1086+
1087+
if (string && string.indexOf("<head") >= 0) {
1088+
new_html += "<head>" + head + "</head>";
1089+
} else {
1090+
new_html += head;
1091+
}
1092+
1093+
if (string && string.indexOf("<body") >= 0) {
1094+
new_html += "<body>" + body + "</body>";
1095+
} else {
1096+
new_html += body;
1097+
}
1098+
1099+
if (string && string.indexOf("<html") >= 0) {
1100+
new_html = "<html>" + new_html + "</html>";
10791101
}
10801102

10811103
return new_html;
@@ -1614,8 +1636,9 @@ var wombat_internal = function($wbwindow) {
16141636

16151637
var new_write = function(string) {
16161638
new_buff = rewrite_html(string);
1617-
orig_doc_write.call(this, new_buff);
1639+
var res = orig_doc_write.call(this, new_buff);
16181640
check_wombat(this.defaultView);
1641+
return res;
16191642
}
16201643

16211644
$wbwindow.document.write = new_write;
@@ -1625,12 +1648,13 @@ var wombat_internal = function($wbwindow) {
16251648
var orig_doc_open = $wbwindow.document.open;
16261649

16271650
var new_open = function() {
1628-
orig_doc_open.call(this);
1651+
var res = orig_doc_open.call(this);
16291652
check_wombat(this.defaultView);
1653+
return res;
16301654
}
16311655

1632-
$wbwindow.document.write = new_open;
1633-
$wbwindow.Document.prototype.write = new_open;
1656+
$wbwindow.document.open = new_open;
1657+
$wbwindow.Document.prototype.open = new_open;
16341658
}
16351659

16361660
//============================================
@@ -1719,8 +1743,8 @@ var wombat_internal = function($wbwindow) {
17191743
wb_info = wbinfo;
17201744

17211745
wb_replay_prefix = wbinfo.prefix;
1722-
if (wb_replay_prefix.indexOf($wbwindow.location.origin) == 0) {
1723-
wb_coll_prefix = wb_replay_prefix.substring($wbwindow.location.origin.length + 1);
1746+
if (wb_replay_prefix.indexOf($wbwindow.top.location.origin) == 0) {
1747+
wb_coll_prefix = wb_replay_prefix.substring($wbwindow.top.location.origin.length + 1);
17241748
} else {
17251749
wb_coll_prefix = wb_replay_prefix;
17261750
}
@@ -1874,6 +1898,7 @@ var wombat_internal = function($wbwindow) {
18741898

18751899
// expose functions
18761900
this.extract_orig = extract_orig;
1901+
this.rewrite_url = rewrite_url;
18771902
this.watch_elem = watch_elem;
18781903
}
18791904

0 commit comments

Comments
 (0)