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);
@ -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,31 +15,23 @@ 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+/);
if (hostWithPort) {
var hostName = hostWithPort[0].substr(0, hostWithPort[0].lastIndexOf(':'));
return hostName + str.substr(hostWithPort[0].length);
}
return str;
}
function WB_IsHostUrl(str)
{
// Good guess that's its a hostname // Good guess that's its a hostname
if (str.indexOf("www.") == 0) { if (str.indexOf("www.") == 0) {
return true; return true;
@ -58,12 +50,12 @@ function WB_IsHostUrl(str)
} }
return false; return false;
} }
function WB_RewriteUrl(url) //============================================
{ function rewrite_url(url) {
var httpPrefix = "http://"; var http_prefix = "http://";
var httpsPrefix = "https://"; var https_prefix = "https://";
// If not dealing with a string, just return it // If not dealing with a string, just return it
if (!url || (typeof url) != "string") { if (!url || (typeof url) != "string") {
@ -71,8 +63,9 @@ function WB_RewriteUrl(url)
} }
// If starts with prefix, no rewriting needed // If starts with prefix, no rewriting needed
// Only check replay prefix (no date) as date may be different for each capture // Only check replay prefix (no date) as date may be different for each
if (url.indexOf(WB_wombat_replayPrefix) == 0) { // capture
if (url.indexOf(wb_replay_prefix) == 0) {
return url; return url;
} }
@ -80,42 +73,42 @@ function WB_RewriteUrl(url)
if (url.charAt(0) == "/") { if (url.charAt(0) == "/") {
// Already a relative url, don't make any changes! // Already a relative url, don't make any changes!
if (url.indexOf(WB_wombat_captureDatePart) >= 0) { if (url.indexOf(wb_capture_date_part) >= 0) {
return url; return url;
} }
return WB_wombat_replayDatePrefix + WB_wombat_origHost + url; return wb_replay_date_prefix + wb_orig_host + url;
} }
// If full url starting with http://, add prefix // If full url starting with http://, add prefix
if (url.indexOf(httpPrefix) == 0 || url.indexOf(httpsPrefix) == 0) { if (url.indexOf(http_prefix) == 0 || url.indexOf(https_prefix) == 0) {
return WB_wombat_replayDatePrefix + url; return wb_replay_date_prefix + url;
} }
// May or may not be a hostname, call function to determine // May or may not be a hostname, call function to determine
// If it is, add the prefix and make sure port is removed // If it is, add the prefix and make sure port is removed
if (WB_IsHostUrl(url)) { if (is_host_url(url)) {
return WB_wombat_replayDatePrefix + httpPrefix + url; return wb_replay_date_prefix + http_prefix + url;
} }
return url; return url;
} }
function WB_CopyObjectFields(obj) //============================================
{ function copy_object_fields(obj) {
var newObj = {}; var new_obj = {};
for (prop in obj) { for (prop in obj) {
if ((typeof obj[prop]) != "function") { if ((typeof obj[prop]) != "function") {
newObj[prop] = obj[prop]; new_obj[prop] = obj[prop];
} }
} }
return newObj; return new_obj;
} }
function WB_ExtractOrig(href) //============================================
{ function extract_orig(href) {
if (!href) { if (!href) {
return ""; return "";
} }
@ -126,177 +119,186 @@ function WB_ExtractOrig(href)
} else { } else {
return href; return href;
} }
} }
function WB_CopyLocationObj(loc) //============================================
{ function copy_location_obj(loc) {
var newLoc = WB_CopyObjectFields(loc); var new_loc = copy_object_fields(loc);
newLoc._origLoc = loc; new_loc._orig_loc = loc;
newLoc._origHref = loc.href; new_loc._orig_href = loc.href;
// Rewrite replace and assign functions // Rewrite replace and assign functions
newLoc.replace = function(url) { this._origLoc.replace(WB_RewriteUrl(url)); } new_loc.replace = function(url) {
newLoc.assign = function(url) { this._origLoc.assign(WB_RewriteUrl(url)); } this._orig_loc.replace(rewrite_url(url));
newLoc.reload = loc.reload; }
new_loc.assign = function(url) {
this._orig_loc.assign(rewrite_url(url));
}
new_loc.reload = loc.reload;
// Adapted from: // Adapted from:
// https://gist.github.com/jlong/2428561 // https://gist.github.com/jlong/2428561
var parser = document.createElement('a'); var parser = document.createElement('a');
parser.href = WB_ExtractOrig(newLoc._origHref); parser.href = extract_orig(new_loc._orig_href);
newLoc.hash = parser.hash; new_loc.hash = parser.hash;
newLoc.host = parser.host; new_loc.host = parser.host;
newLoc.hostname = parser.hostname; new_loc.hostname = parser.hostname;
newLoc.href = parser.href; new_loc.href = parser.href;
if (newLoc.origin) { if (new_loc.origin) {
newLoc.origin = parser.origin; new_loc.origin = parser.origin;
} }
newLoc.pathname = parser.pathname; new_loc.pathname = parser.pathname;
newLoc.port = parser.port new_loc.port = parser.port
newLoc.protocol = parser.protocol; new_loc.protocol = parser.protocol;
newLoc.search = parser.search; new_loc.search = parser.search;
newLoc.toString = function() { return this.href; } new_loc.toString = function() {
return this.href;
return newLoc;
}
function WB_wombat_updateLoc(reqHref, origHref, location)
{
if (reqHref && (WB_ExtractOrig(origHref) != WB_ExtractOrig(reqHref))) {
var finalHref = WB_RewriteUrl(reqHref);
location.href = finalHref;
} }
}
function WB_wombat_checkLocationChange(wbLoc, isTop) return new_loc;
{ }
var locType = (typeof wbLoc);
var location = (isTop ? window.top.location : window.location); //============================================
function update_location(req_href, orig_href, location) {
if (req_href && (extract_orig(orig_href) != extract_orig(req_href))) {
var final_href = rewrite_url(req_href);
location.href = final_href;
}
}
//============================================
function check_location_change(loc, is_top) {
var locType = (typeof loc);
var location = (is_top ? window.top.location : window.location);
// String has been assigned to location, so assign it // String has been assigned to location, so assign it
if (locType == "string") { if (locType == "string") {
WB_wombat_updateLoc(wbLoc, location.href, location) update_location(loc, location.href, location)
} else if (locType == "object") { } else if (locType == "object") {
WB_wombat_updateLoc(wbLoc.href, wbLoc._origHref, location); update_location(loc.href, loc._orig_href, location);
}
} }
}
var wombat_updating = false; //============================================
function check_all_locations() {
function WB_wombat_checkLocations() if (wb_wombat_updating) {
{
if (wombat_updating) {
return false; return false;
} }
wombat_updating = true; wb_wombat_updating = true;
WB_wombat_checkLocationChange(window.WB_wombat_location, false); check_location_change(window.WB_wombat_location, false);
if (window.self.location != window.top.location) { if (window.self.location != window.top.location) {
WB_wombat_checkLocationChange(window.top.WB_wombat_location, true); check_location_change(window.top.WB_wombat_location, true);
} }
wombat_updating = false; wb_wombat_updating = false;
} }
function WB_wombat_Init_SeededRandom(seed) //============================================
{ function init_seeded_random(seed) {
// Adapted from: // Adapted from:
// http://indiegamr.com/generate-repeatable-random-numbers-in-js/ // http://indiegamr.com/generate-repeatable-random-numbers-in-js/
Math.seed = seed; Math.seed = seed;
function seededRandom() { function seeded_random() {
Math.seed = (Math.seed * 9301 + 49297) % 233280; Math.seed = (Math.seed * 9301 + 49297) % 233280;
var rnd = Math.seed / 233280; var rnd = Math.seed / 233280;
return rnd; return rnd;
} }
Math.random = seededRandom; Math.random = seeded_random;
} }
function WB_wombat_CopyHistoryFunc(history, funcName) //============================================
{ function copy_history_func(history, func_name) {
origFunc = history[funcName]; orig_func = history[func_name];
if (!origFunc) { if (!orig_func) {
return; return;
} }
history['_orig_' + funcName] = origFunc; history['_orig_' + func_name] = orig_func;
function rewrittenFunc(stateObj, title, url) { function rewritten_func(state_obj, title, url) {
url = WB_RewriteUrl(url); url = rewrite_url(url);
return origFunc.call(history, stateObj, title, url); return orig_func.call(history, state_obj, title, url);
} }
history[funcName] = rewrittenFunc; history[func_name] = rewritten_func;
return rewrittenFunc; return rewritten_func;
} }
function WB_wombat_Init_AjaxOverride() { //============================================
if (!window.XMLHttpRequest || !window.XMLHttpRequest.prototype || function init_ajax_rewrite() {
if (!window.XMLHttpRequest ||
!window.XMLHttpRequest.prototype ||
!window.XMLHttpRequest.prototype.open) { !window.XMLHttpRequest.prototype.open) {
return; return;
} }
var orig = window.XMLHttpRequest.prototype.open; var orig = window.XMLHttpRequest.prototype.open;
function openRewritten(sMethod, sUrl, bAsync, sUser, sPassword) { function open_rewritten(method, url, async, user, password) {
sUrl = WB_RewriteUrl(sUrl); url = rewrite_url(url);
return orig.call(this, sMethod, sUrl, bAsync, sUser, sPassword); return orig.call(this, method, url, async, user, password);
} }
window.XMLHttpRequest.prototype.open = openRewritten; window.XMLHttpRequest.prototype.open = open_rewritten;
} }
function WB_wombat_Init(replayPrefix, captureDate, origHost, timestamp) //============================================
{ function wombat_init(replay_prefix, capture_date, orig_host, timestamp) {
WB_wombat_replayPrefix = replayPrefix; wb_replay_prefix = replay_prefix;
WB_wombat_replayDatePrefix = replayPrefix + captureDate + "/"; wb_replay_date_prefix = replay_prefix + capture_date + "/";
WB_wombat_captureDatePart = "/" + captureDate + "/"; wb_capture_date_part = "/" + capture_date + "/";
WB_wombat_origHost = "http://" + origHost; wb_orig_host = "http://" + orig_host;
// Location // Location
window.WB_wombat_location = WB_CopyLocationObj(window.self.location); window.WB_wombat_location = copy_location_obj(window.self.location);
document.WB_wombat_location = window.WB_wombat_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') }}");