mirror of
https://github.com/webrecorder/pywb.git
synced 2025-03-24 06:59:52 +01:00
wombat: improved WombatLocation, changes to any location property propagate to location object.
all properties overriden to check if location changed with pushState/replaceState
This commit is contained in:
parent
c2f99d6cfd
commit
bbd987060e
@ -282,6 +282,11 @@ var wombat_internal = function(window) {
|
|||||||
|
|
||||||
href = href.toString();
|
href = href.toString();
|
||||||
|
|
||||||
|
// ignore certain urls
|
||||||
|
if (starts_with(href, IGNORE_PREFIXES)) {
|
||||||
|
return href;
|
||||||
|
}
|
||||||
|
|
||||||
var index = href.indexOf("/http", 1);
|
var index = href.indexOf("/http", 1);
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
index = href.indexOf("///", 1);
|
index = href.indexOf("///", 1);
|
||||||
@ -342,9 +347,9 @@ var wombat_internal = function(window) {
|
|||||||
//============================================
|
//============================================
|
||||||
//Define WombatLocation
|
//Define WombatLocation
|
||||||
|
|
||||||
function WombatLocation(loc) {
|
function WombatLocation(orig_loc) {
|
||||||
this._orig_loc = loc;
|
this._orig_loc = orig_loc;
|
||||||
this._orig_href = loc.href;
|
this._orig_href = orig_loc.href;
|
||||||
|
|
||||||
|
|
||||||
// Rewrite replace and assign functions
|
// Rewrite replace and assign functions
|
||||||
@ -366,80 +371,115 @@ var wombat_internal = function(window) {
|
|||||||
return this._orig_loc.assign(new_url);
|
return this._orig_loc.assign(new_url);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.reload = loc.reload;
|
this.reload = orig_loc.reload;
|
||||||
|
|
||||||
// Adapted from:
|
this._parser = window.document.createElement('a', true);
|
||||||
// https://gist.github.com/jlong/2428561
|
|
||||||
var parser = window.document.createElement('a', true);
|
|
||||||
var href = extract_orig(this._orig_href);
|
|
||||||
parser.href = href;
|
|
||||||
|
|
||||||
this._autooverride = false;
|
var init_loc = function(loc, orig_href) {
|
||||||
|
// Adapted from:
|
||||||
|
// https://gist.github.com/jlong/2428561
|
||||||
|
|
||||||
var _set_hash = function(hash) {
|
var href = extract_orig(orig_href);
|
||||||
this._orig_loc.hash = hash;
|
var parser = loc._parser;
|
||||||
return this._orig_loc.hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
var _get_hash = function() {
|
parser.href = href;
|
||||||
return this._orig_loc.hash;
|
|
||||||
}
|
|
||||||
|
|
||||||
var _get_href = function() {
|
href = parser.getAttribute("href");
|
||||||
return extract_orig(this._orig_loc.href);
|
loc._hash = parser.hash;
|
||||||
//return extract_orig(this._orig_href);
|
|
||||||
}
|
|
||||||
|
|
||||||
href = parser.getAttribute("href");
|
/*if (hash) {
|
||||||
var hash = parser.hash;
|
var hidx = href.lastIndexOf("#");
|
||||||
|
if (hidx > 0) {
|
||||||
|
href = href.substring(0, hidx);
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
if (hash) {
|
loc._href = href;
|
||||||
var hidx = href.lastIndexOf("#");
|
|
||||||
if (hidx > 0) {
|
loc._host = parser.host;
|
||||||
href = href.substring(0, hidx);
|
loc._hostname = parser.hostname;
|
||||||
|
|
||||||
|
if (parser.origin) {
|
||||||
|
loc._origin = parser.origin;
|
||||||
|
}
|
||||||
|
|
||||||
|
loc._pathname = parser.pathname;
|
||||||
|
loc._port = parser.port
|
||||||
|
//this.protocol = parser.protocol;
|
||||||
|
loc._protocol = loc._orig_loc.protocol;
|
||||||
|
loc._search = parser.search;
|
||||||
|
|
||||||
|
if (!Object.defineProperty) {
|
||||||
|
loc.href = href;
|
||||||
|
loc.hash = parser.hash;
|
||||||
|
|
||||||
|
loc.host = loc._host;
|
||||||
|
loc.hostname = loc._hostname;
|
||||||
|
loc.origin = loc._origin;
|
||||||
|
loc.pathname = loc._pathname;
|
||||||
|
loc.port = loc._port;
|
||||||
|
loc.protocol = loc._protocol;
|
||||||
|
loc.search = loc._search;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var make_get_loc_prop = function(loc, prop) {
|
||||||
|
function getter() {
|
||||||
|
if (loc._orig_href != loc._orig_loc.href) {
|
||||||
|
init_loc(loc, loc._orig_loc.href);
|
||||||
|
}
|
||||||
|
|
||||||
|
return loc["_" + prop];
|
||||||
|
}
|
||||||
|
|
||||||
|
return getter;
|
||||||
|
}
|
||||||
|
|
||||||
|
var make_set_loc_prop = function(loc, prop) {
|
||||||
|
function setter(value) {
|
||||||
|
loc["_" + prop] = value;
|
||||||
|
loc._parser[prop] = value;
|
||||||
|
|
||||||
|
if (prop == "hash") {
|
||||||
|
loc._orig_loc.hash = hash;
|
||||||
|
} else {
|
||||||
|
loc._orig_loc.href = rewrite_url(loc._parser.href);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return setter;
|
||||||
|
}
|
||||||
|
|
||||||
|
function add_loc_prop(loc, prop) {
|
||||||
|
def_prop(loc, prop, make_set_loc_prop(loc, prop), make_get_loc_prop(loc, prop));
|
||||||
|
}
|
||||||
|
|
||||||
|
init_loc(this, orig_loc.href);
|
||||||
|
|
||||||
if (Object.defineProperty) {
|
if (Object.defineProperty) {
|
||||||
var res1 = def_prop(this, "href",
|
add_loc_prop(this, "href");
|
||||||
this.assign,
|
add_loc_prop(this, "hash");
|
||||||
_get_href);
|
add_loc_prop(this, "host");
|
||||||
|
add_loc_prop(this, "hostname");
|
||||||
var res2 = def_prop(this, "hash",
|
add_loc_prop(this, "pathname");
|
||||||
_set_hash,
|
add_loc_prop(this, "origin");
|
||||||
_get_hash);
|
add_loc_prop(this, "port");
|
||||||
|
add_loc_prop(this, "protocol");
|
||||||
this._autooverride = res1 && res2;
|
add_loc_prop(this, "search");
|
||||||
} else {
|
|
||||||
this.href = href;
|
|
||||||
this.hash = parser.hash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.host = parser.host;
|
|
||||||
this.hostname = parser.hostname;
|
|
||||||
|
|
||||||
if (parser.origin) {
|
|
||||||
this.origin = parser.origin;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.pathname = parser.pathname;
|
|
||||||
this.port = parser.port
|
|
||||||
//this.protocol = parser.protocol;
|
|
||||||
this.protocol = window.location.protocol;
|
|
||||||
this.search = parser.search;
|
|
||||||
|
|
||||||
this.toString = function() {
|
this.toString = function() {
|
||||||
return this.href;
|
return this.href;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copy any remaining properties
|
// Copy any remaining properties
|
||||||
for (prop in loc) {
|
for (prop in orig_loc) {
|
||||||
if (this.hasOwnProperty(prop)) {
|
if (this.hasOwnProperty(prop)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((typeof loc[prop]) != "function") {
|
if ((typeof orig_loc[prop]) != "function") {
|
||||||
this[prop] = loc[prop];
|
this[prop] = orig_loc[prop];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1173,7 +1213,7 @@ var wombat_internal = function(window) {
|
|||||||
// Location
|
// Location
|
||||||
var wombat_location = new WombatLocation(win.location);
|
var wombat_location = new WombatLocation(win.location);
|
||||||
|
|
||||||
if (wombat_location._autooverride) {
|
if (Object.defineProperty) {
|
||||||
|
|
||||||
var setter = function(value) {
|
var setter = function(value) {
|
||||||
if (this._WB_wombat_location) {
|
if (this._WB_wombat_location) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user