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

fixup wb and wombat.js:

fix formatting to 4-tab snake_case, remove obsolete code
This commit is contained in:
Ilya Kreymer 2014-03-10 00:55:41 -07:00
parent e3d700a50f
commit 3322fb233f
3 changed files with 251 additions and 251 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright(c) 2013-2014 Ilya Kreymer. Released under the GNU General Public License. Copyright(c) 2013-2014 Internet Archive / Ilya Kreymer. Released under the GNU General Public License.
This file is part of pywb. This file is part of pywb.
@ -15,10 +15,9 @@ This file is part of pywb.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with pywb. If not, see <http://www.gnu.org/licenses/>. along with pywb. If not, see <http://www.gnu.org/licenses/>.
*/ */
function initBanner() function init_banner() {
{
var BANNER_ID = "_wayback_banner"; var BANNER_ID = "_wayback_banner";
var banner = document.getElementById(BANNER_ID); var banner = document.getElementById(BANNER_ID);
@ -26,7 +25,7 @@ function initBanner()
if (!banner) { if (!banner) {
banner = document.createElement("wb_div"); banner = document.createElement("wb_div");
banner.setAttribute("id", BANNER_ID); banner.setAttribute("id", BANNER_ID);
banner.setAttribute("lang", "en"); banner.setAttribute("lang", "en");
text = "This is an archived page "; text = "This is an archived page ";
if (wbinfo && wbinfo.capture_str) { if (wbinfo && wbinfo.capture_str) {
@ -39,12 +38,11 @@ function initBanner()
} }
var readyStateCheckInterval = setInterval(function() { var readyStateCheckInterval = setInterval(function() {
if (document.readyState === "interactive" || document.readyState === "complete") { if (document.readyState === "interactive" ||
initBanner(); document.readyState === "complete") {
init_banner();
clearInterval(readyStateCheckInterval); clearInterval(readyStateCheckInterval);
} }
}, 10); }, 10);

View File

@ -1,5 +1,5 @@
/* /*
Copyright(c) 2013-2014 Ilya Kreymer. Released under the GNU General Public License. Copyright(c) 2013-2014 Internet Archive / Ilya Kreymer. Released under the GNU General Public License.
This file is part of pywb. This file is part of pywb.
@ -15,288 +15,290 @@ This file is part of pywb.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with pywb. If not, see <http://www.gnu.org/licenses/>. along with pywb. If not, see <http://www.gnu.org/licenses/>.
*/ */
//============================================ //============================================
// Wombat JS-Rewriting Library // Wombat JS-Rewriting Library
//============================================ //============================================
WB_wombat_init = (function() {
var WB_wombat_replayPrefix; // Globals
var WB_wombat_replayDatePrefix; var wb_replay_prefix;
var WB_wombat_captureDatePart; var wb_replay_date_prefix;
var WB_wombat_origHost; var wb_capture_date_part;
var wb_orig_host;
var wb_wombat_updating = false;
function WB_StripPort(str) //============================================
{ function is_host_url(str) {
var hostWithPort = str.match(/^http:\/\/[\w\d@.-]+:\d+/); // Good guess that's its a hostname
if (hostWithPort) { if (str.indexOf("www.") == 0) {
var hostName = hostWithPort[0].substr(0, hostWithPort[0].lastIndexOf(':')); return true;
return hostName + str.substr(hostWithPort[0].length); }
}
return str; // hostname:port (port required)
} var matches = str.match(/^[\w-]+(\.[\w-_]+)+(:\d+)(\/|$)/);
if (matches && (matches[0].length < 64)) {
return true;
}
function WB_IsHostUrl(str) // ip:port
{ matches = str.match(/^\d+\.\d+\.\d+\.\d+(:\d+)?(\/|$)/);
// Good guess that's its a hostname if (matches && (matches[0].length < 64)) {
if (str.indexOf("www.") == 0) { return true;
return true; }
}
// hostname:port (port required) return false;
var matches = str.match(/^[\w-]+(\.[\w-_]+)+(:\d+)(\/|$)/);
if (matches && (matches[0].length < 64)) {
return true;
}
// ip:port
matches = str.match(/^\d+\.\d+\.\d+\.\d+(:\d+)?(\/|$)/);
if (matches && (matches[0].length < 64)) {
return true;
}
return false;
}
function WB_RewriteUrl(url)
{
var httpPrefix = "http://";
var httpsPrefix = "https://";
// If not dealing with a string, just return it
if (!url || (typeof url) != "string") {
return url;
}
// If starts with prefix, no rewriting needed
// Only check replay prefix (no date) as date may be different for each capture
if (url.indexOf(WB_wombat_replayPrefix) == 0) {
return url;
}
// If server relative url, add prefix and original host
if (url.charAt(0) == "/") {
// Already a relative url, don't make any changes!
if (url.indexOf(WB_wombat_captureDatePart) >= 0) {
return url;
} }
return WB_wombat_replayDatePrefix + WB_wombat_origHost + url; //============================================
} function rewrite_url(url) {
var http_prefix = "http://";
var https_prefix = "https://";
// If full url starting with http://, add prefix // If not dealing with a string, just return it
if (url.indexOf(httpPrefix) == 0 || url.indexOf(httpsPrefix) == 0) { if (!url || (typeof url) != "string") {
return WB_wombat_replayDatePrefix + url; return url;
} }
// May or may not be a hostname, call function to determine // If starts with prefix, no rewriting needed
// If it is, add the prefix and make sure port is removed // Only check replay prefix (no date) as date may be different for each
if (WB_IsHostUrl(url)) { // capture
return WB_wombat_replayDatePrefix + httpPrefix + url; if (url.indexOf(wb_replay_prefix) == 0) {
} return url;
}
return url; // If server relative url, add prefix and original host
} if (url.charAt(0) == "/") {
function WB_CopyObjectFields(obj) // Already a relative url, don't make any changes!
{ if (url.indexOf(wb_capture_date_part) >= 0) {
var newObj = {}; return url;
}
for (prop in obj) { return wb_replay_date_prefix + wb_orig_host + url;
if ((typeof obj[prop]) != "function") { }
newObj[prop] = obj[prop];
// If full url starting with http://, add prefix
if (url.indexOf(http_prefix) == 0 || url.indexOf(https_prefix) == 0) {
return wb_replay_date_prefix + url;
}
// May or may not be a hostname, call function to determine
// If it is, add the prefix and make sure port is removed
if (is_host_url(url)) {
return wb_replay_date_prefix + http_prefix + url;
}
return url;
} }
}
return newObj; //============================================
} function copy_object_fields(obj) {
var new_obj = {};
function WB_ExtractOrig(href) for (prop in obj) {
{ if ((typeof obj[prop]) != "function") {
if (!href) { new_obj[prop] = obj[prop];
return ""; }
} }
href = href.toString();
var index = href.indexOf("/http", 1);
if (index > 0) {
return href.substr(index + 1);
} else {
return href;
}
}
function WB_CopyLocationObj(loc) return new_obj;
{ }
var newLoc = WB_CopyObjectFields(loc);
newLoc._origLoc = loc; //============================================
newLoc._origHref = loc.href; function extract_orig(href) {
if (!href) {
return "";
}
href = href.toString();
var index = href.indexOf("/http", 1);
if (index > 0) {
return href.substr(index + 1);
} else {
return href;
}
}
// Rewrite replace and assign functions //============================================
newLoc.replace = function(url) { this._origLoc.replace(WB_RewriteUrl(url)); } function copy_location_obj(loc) {
newLoc.assign = function(url) { this._origLoc.assign(WB_RewriteUrl(url)); } var new_loc = copy_object_fields(loc);
newLoc.reload = loc.reload;
// Adapted from: new_loc._orig_loc = loc;
// https://gist.github.com/jlong/2428561 new_loc._orig_href = loc.href;
var parser = document.createElement('a');
parser.href = WB_ExtractOrig(newLoc._origHref);
newLoc.hash = parser.hash; // Rewrite replace and assign functions
newLoc.host = parser.host; new_loc.replace = function(url) {
newLoc.hostname = parser.hostname; this._orig_loc.replace(rewrite_url(url));
newLoc.href = parser.href; }
new_loc.assign = function(url) {
this._orig_loc.assign(rewrite_url(url));
}
new_loc.reload = loc.reload;
if (newLoc.origin) { // Adapted from:
newLoc.origin = parser.origin; // https://gist.github.com/jlong/2428561
} var parser = document.createElement('a');
parser.href = extract_orig(new_loc._orig_href);
newLoc.pathname = parser.pathname; new_loc.hash = parser.hash;
newLoc.port = parser.port new_loc.host = parser.host;
newLoc.protocol = parser.protocol; new_loc.hostname = parser.hostname;
newLoc.search = parser.search; new_loc.href = parser.href;
newLoc.toString = function() { return this.href; } if (new_loc.origin) {
new_loc.origin = parser.origin;
}
return newLoc; new_loc.pathname = parser.pathname;
} new_loc.port = parser.port
new_loc.protocol = parser.protocol;
new_loc.search = parser.search;
function WB_wombat_updateLoc(reqHref, origHref, location) new_loc.toString = function() {
{ return this.href;
if (reqHref && (WB_ExtractOrig(origHref) != WB_ExtractOrig(reqHref))) { }
var finalHref = WB_RewriteUrl(reqHref);
location.href = finalHref; return new_loc;
} }
}
function WB_wombat_checkLocationChange(wbLoc, isTop) //============================================
{ function update_location(req_href, orig_href, location) {
var locType = (typeof wbLoc); if (req_href && (extract_orig(orig_href) != extract_orig(req_href))) {
var final_href = rewrite_url(req_href);
var location = (isTop ? window.top.location : window.location); location.href = final_href;
}
}
// String has been assigned to location, so assign it //============================================
if (locType == "string") { function check_location_change(loc, is_top) {
WB_wombat_updateLoc(wbLoc, location.href, location) var locType = (typeof loc);
} else if (locType == "object") { var location = (is_top ? window.top.location : window.location);
WB_wombat_updateLoc(wbLoc.href, wbLoc._origHref, location);
}
}
var wombat_updating = false; // String has been assigned to location, so assign it
if (locType == "string") {
update_location(loc, location.href, location)
function WB_wombat_checkLocations() } else if (locType == "object") {
{ update_location(loc.href, loc._orig_href, location);
if (wombat_updating) { }
return false; }
}
wombat_updating = true; //============================================
function check_all_locations() {
if (wb_wombat_updating) {
return false;
}
WB_wombat_checkLocationChange(window.WB_wombat_location, false); wb_wombat_updating = true;
if (window.self.location != window.top.location) { check_location_change(window.WB_wombat_location, false);
WB_wombat_checkLocationChange(window.top.WB_wombat_location, true);
}
wombat_updating = false; if (window.self.location != window.top.location) {
} check_location_change(window.top.WB_wombat_location, true);
}
function WB_wombat_Init_SeededRandom(seed) wb_wombat_updating = false;
{ }
// Adapted from:
// http://indiegamr.com/generate-repeatable-random-numbers-in-js/
Math.seed = seed; //============================================
function seededRandom() { function init_seeded_random(seed) {
Math.seed = (Math.seed * 9301 + 49297) % 233280; // Adapted from:
var rnd = Math.seed / 233280; // http://indiegamr.com/generate-repeatable-random-numbers-in-js/
return rnd; Math.seed = seed;
} function seeded_random() {
Math.seed = (Math.seed * 9301 + 49297) % 233280;
var rnd = Math.seed / 233280;
Math.random = seededRandom; return rnd;
} }
function WB_wombat_CopyHistoryFunc(history, funcName) Math.random = seeded_random;
{ }
origFunc = history[funcName];
if (!origFunc) { //============================================
return; function copy_history_func(history, func_name) {
} orig_func = history[func_name];
history['_orig_' + funcName] = origFunc; if (!orig_func) {
return;
}
function rewrittenFunc(stateObj, title, url) { history['_orig_' + func_name] = orig_func;
url = WB_RewriteUrl(url);
return origFunc.call(history, stateObj, title, url);
}
history[funcName] = rewrittenFunc; function rewritten_func(state_obj, title, url) {
url = rewrite_url(url);
return orig_func.call(history, state_obj, title, url);
}
return rewrittenFunc; history[func_name] = rewritten_func;
}
function WB_wombat_Init_AjaxOverride() { return rewritten_func;
if (!window.XMLHttpRequest || !window.XMLHttpRequest.prototype || }
!window.XMLHttpRequest.prototype.open) {
return;
}
var orig = window.XMLHttpRequest.prototype.open; //============================================
function init_ajax_rewrite() {
if (!window.XMLHttpRequest ||
!window.XMLHttpRequest.prototype ||
!window.XMLHttpRequest.prototype.open) {
return;
}
function openRewritten(sMethod, sUrl, bAsync, sUser, sPassword) { var orig = window.XMLHttpRequest.prototype.open;
sUrl = WB_RewriteUrl(sUrl);
return orig.call(this, sMethod, sUrl, bAsync, sUser, sPassword);
}
window.XMLHttpRequest.prototype.open = openRewritten; function open_rewritten(method, url, async, user, password) {
} url = rewrite_url(url);
return orig.call(this, method, url, async, user, password);
}
function WB_wombat_Init(replayPrefix, captureDate, origHost, timestamp) window.XMLHttpRequest.prototype.open = open_rewritten;
{ }
WB_wombat_replayPrefix = replayPrefix;
WB_wombat_replayDatePrefix = replayPrefix + captureDate + "/";
WB_wombat_captureDatePart = "/" + captureDate + "/";
WB_wombat_origHost = "http://" + origHost; //============================================
function wombat_init(replay_prefix, capture_date, orig_host, timestamp) {
wb_replay_prefix = replay_prefix;
wb_replay_date_prefix = replay_prefix + capture_date + "/";
wb_capture_date_part = "/" + capture_date + "/";
// Location wb_orig_host = "http://" + orig_host;
window.WB_wombat_location = WB_CopyLocationObj(window.self.location);
document.WB_wombat_location = window.WB_wombat_location;
// Location
window.WB_wombat_location = copy_location_obj(window.self.location);
document.WB_wombat_location = window.WB_wombat_location;
if (window.self.location != window.top.location) { if (window.self.location != window.top.location) {
window.top.WB_wombat_location = WB_CopyLocationObj(window.top.location); window.top.WB_wombat_location = copy_location_obj(window.top.location);
} }
if (window.opener) { if (window.opener) {
window.opener.WB_wombat_location = (window.opener ? WB_CopyLocationObj(window.opener.location) : null); window.opener.WB_wombat_location = (window.opener ? copy_location_obj(window.opener.location)
} : null);
}
// Domain // Domain
document.WB_wombat_domain = origHost; document.WB_wombat_domain = orig_host;
// History // History
WB_wombat_CopyHistoryFunc(window.history, 'pushState'); copy_history_func(window.history, 'pushState');
WB_wombat_CopyHistoryFunc(window.history, 'replaceState'); copy_history_func(window.history, 'replaceState');
// Ajax // Ajax
WB_wombat_Init_AjaxOverride(); init_ajax_rewrite();
// Random // Random
WB_wombat_Init_SeededRandom(timestamp); init_seeded_random(timestamp);
} }
// Check quickly after page load // Check quickly after page load
setTimeout(WB_wombat_checkLocations, 100); setTimeout(check_all_locations, 100);
// Check periodically every few seconds
setInterval(check_all_locations, 500);
// Check periodically every few seconds return wombat_init;
setInterval(WB_wombat_checkLocations, 500);
})(this);

View File

@ -2,7 +2,7 @@
{% if rule.js_rewrite_location %} {% if rule.js_rewrite_location %}
<script src='{{ wbrequest.host_prefix }}/static/default/wombat.js'> </script> <script src='{{ wbrequest.host_prefix }}/static/default/wombat.js'> </script>
<script> <script>
WB_wombat_Init("{{wbrequest.wb_prefix}}", WB_wombat_init("{{wbrequest.wb_prefix}}",
"{{cdx['timestamp']}}", "{{cdx['timestamp']}}",
"{{cdx['original'] | host}}", "{{cdx['original'] | host}}",
"{{ cdx.timestamp | format_ts('%s') }}"); "{{ cdx.timestamp | format_ts('%s') }}");