Skip to content

Commit 69f8382

Browse files
committed
wombat: continue using anchor element (with no rewriting) instead of less standard URL class, avoid
edge cases/browser differences
1 parent f62bf9a commit 69f8382

File tree

1 file changed

+50
-47
lines changed

1 file changed

+50
-47
lines changed

pywb/static/wombat.js

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,6 @@ var wombat_internal = function($wbwindow) {
4848
// custom options
4949
var wb_opts;
5050

51-
//============================================
52-
function load_url_js() {
53-
var s = $wbwindow.document.createElement("script");
54-
s.src = wbinfo.static_prefix + "/url.js";
55-
$wbwindow.document.head.appendChild(s);
56-
}
57-
58-
if (!$wbwindow.URL) {
59-
load_url_js();
60-
}
61-
6251
//============================================
6352
function is_host_url(str) {
6453
// Good guess that's its a hostname
@@ -156,6 +145,8 @@ var wombat_internal = function($wbwindow) {
156145

157146
var REWRITE_ATTRS = ["src", "href", "poster"];
158147

148+
var URL_PROPS = ["href", "hash", "pathname", "host", "hostname", "protocol", "origin", "search", "port"];
149+
159150
//============================================
160151
function rewrite_url_(url) {
161152
// If undefined, just return it
@@ -366,16 +357,9 @@ var wombat_internal = function($wbwindow) {
366357
function make_parser(href) {
367358
href = extract_orig(href);
368359

369-
if (!$wbwindow.URL) {
370-
var p = $wbwindow.document.createElement("a");
371-
p.href = href;
372-
return p;
373-
}
374-
375-
if (href.indexOf("//") == 0) {
376-
href = $wbwindow.location.protocol + href;
377-
}
378-
return new $wbwindow.URL(href);
360+
var p = $wbwindow.document.createElement("a", true);
361+
p.href = href;
362+
return p;
379363
}
380364

381365

@@ -422,7 +406,11 @@ var wombat_internal = function($wbwindow) {
422406
function init_loc_override(loc_obj, orig_setter, orig_getter) {
423407
var make_get_loc_prop = function(prop) {
424408
function getter() {
425-
var curr_orig_href = orig_getter.call(this);
409+
if (this._no_rewrite) {
410+
return orig_getter.call(this, prop);
411+
}
412+
413+
var curr_orig_href = orig_getter.call(this, "href");
426414

427415
if (prop == "href") {
428416
return extract_orig(curr_orig_href);
@@ -442,13 +430,23 @@ var wombat_internal = function($wbwindow) {
442430

443431
var make_set_loc_prop = function(prop) {
444432
function setter(value) {
433+
if (this._no_rewrite) {
434+
orig_setter.call(this, prop, value);
435+
return;
436+
}
437+
445438
this["_" + prop] = value;
446439

447440
if (!this._parser) {
448441
var href = orig_getter.call(this);
449442
this._parser = make_parser(href);
450443
}
451-
this._parser[prop] = value;
444+
445+
try {
446+
this._parser[prop] = value;
447+
} catch (e) {
448+
console.log('Error setting ' + prop + ' = ' + value);
449+
}
452450

453451
if (prop == "hash") {
454452
value = this._parser[prop];
@@ -468,15 +466,9 @@ var wombat_internal = function($wbwindow) {
468466
}
469467

470468
if (Object.defineProperty) {
471-
add_loc_prop(loc_obj, "href");
472-
add_loc_prop(loc_obj, "hash");
473-
add_loc_prop(loc_obj, "host");
474-
add_loc_prop(loc_obj, "hostname");
475-
add_loc_prop(loc_obj, "pathname");
476-
add_loc_prop(loc_obj, "origin");
477-
add_loc_prop(loc_obj, "port");
478-
add_loc_prop(loc_obj, "protocol");
479-
add_loc_prop(loc_obj, "search");
469+
for (var i = 0; i < URL_PROPS.length; i++) {
470+
add_loc_prop(loc_obj, URL_PROPS[i]);
471+
}
480472
}
481473
}
482474

@@ -508,8 +500,8 @@ var wombat_internal = function($wbwindow) {
508500

509501
this.reload = orig_loc.reload;
510502

511-
this.orig_getter = function() {
512-
return this._orig_loc.href;
503+
this.orig_getter = function(prop) {
504+
return this._orig_loc[prop];
513505
}
514506

515507
this.orig_setter = function(prop, value) {
@@ -1188,22 +1180,33 @@ var wombat_internal = function($wbwindow) {
11881180

11891181
//override_attr($wbwindow.HTMLAnchorElement.prototype, "href");
11901182
//return;
1183+
var anchor_orig = {}
1184+
1185+
function save_prop(prop) {
1186+
anchor_orig["get_" + prop] = get_orig_getter($wbwindow.HTMLAnchorElement.prototype, prop);
1187+
anchor_orig["set_" + prop] = get_orig_setter($wbwindow.HTMLAnchorElement.prototype, prop);
1188+
}
11911189

1192-
var anchor_orig_getter = get_orig_getter($wbwindow.HTMLAnchorElement.prototype, "href");
1193-
var anchor_orig_setter_href = get_orig_setter($wbwindow.HTMLAnchorElement.prototype, "href");
1194-
var anchor_orig_setter_hash = get_orig_setter($wbwindow.HTMLAnchorElement.prototype, "hash");
1190+
for (var i = 0; i < URL_PROPS.length; i++) {
1191+
save_prop(URL_PROPS[i]);
1192+
}
11951193

11961194
var anchor_setter = function(prop, value) {
1197-
if (prop == "href") {
1198-
anchor_orig_setter_href.call(this, value);
1195+
var func = anchor_orig["set_" + prop];
1196+
if (func) {
1197+
return func.call(this, value);
11991198
} else {
1200-
anchor_orig_setter_hash.call(this, value);
1199+
return "";
12011200
}
12021201
}
12031202

1204-
var anchor_getter = function() {
1205-
var value = anchor_orig_getter.call(this);
1206-
return value;
1203+
var anchor_getter = function(prop) {
1204+
var func = anchor_orig["get_" + prop];
1205+
if (func) {
1206+
return func.call(this);
1207+
} else {
1208+
return "";
1209+
}
12071210
}
12081211

12091212
init_loc_override($wbwindow.HTMLAnchorElement.prototype, anchor_setter, anchor_getter);
@@ -1462,8 +1465,8 @@ var wombat_internal = function($wbwindow) {
14621465
/*
14631466
for (var i = 0; i < $wbwindow.frames.length; i++) {
14641467
try {
1465-
init_postmessage_override($wbwindow.frames[i]);
1466-
//$wbwindow.frames[i].postMessage = postmessage_rewritten;
1468+
//init_postmessage_override($wbwindow.frames[i]);
1469+
$wbwindow.frames[i].postMessage = postmessage_rewritten;
14671470
} catch (e) {
14681471
console.log(e);
14691472
}
@@ -1644,9 +1647,9 @@ var wombat_internal = function($wbwindow) {
16441647
win._wb_wombat = new win._WBWombat(wb_info);
16451648
} else {
16461649
// These should get overriden when content is loaded, but just in case...
1647-
//win.WB_wombat_location = win.location;
1650+
//win._WB_wombat_location = win.location;
16481651
//win.document.WB_wombat_location = win.document.location;
1649-
//win.WB_wombat_top = $wbwindow.WB_wombat_top;
1652+
//win._WB_wombat_top = $wbwindow.WB_wombat_top;
16501653

16511654
init_proto_pm_origin(win);
16521655
//init_postmessage_override(win);

0 commit comments

Comments
 (0)