1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-24 06:59:52 +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)
{
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 is_host_url(str) {
// Good guess that's its a hostname
if (str.indexOf("www.") == 0) {
return true;
}
function WB_IsHostUrl(str) // hostname:port (port required)
{ var matches = str.match(/^[\w-]+(\.[\w-_]+)+(:\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)
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; // ip:port
} matches = str.match(/^\d+\.\d+\.\d+\.\d+(:\d+)?(\/|$)/);
if (matches && (matches[0].length < 64)) {
return true;
}
function WB_RewriteUrl(url) return false;
{
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;
}
// If full url starting with http://, add prefix
if (url.indexOf(httpPrefix) == 0 || url.indexOf(httpsPrefix) == 0) {
return WB_wombat_replayDatePrefix + 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 (WB_IsHostUrl(url)) {
return WB_wombat_replayDatePrefix + httpPrefix + url;
}
return url; //============================================
} function rewrite_url(url) {
var http_prefix = "http://";
var https_prefix = "https://";
function WB_CopyObjectFields(obj) // If not dealing with a string, just return it
{ if (!url || (typeof url) != "string") {
var newObj = {}; return url;
}
for (prop in obj) {
if ((typeof obj[prop]) != "function") { // If starts with prefix, no rewriting needed
newObj[prop] = obj[prop]; // Only check replay prefix (no date) as date may be different for each
// capture
if (url.indexOf(wb_replay_prefix) == 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_capture_date_part) >= 0) {
return url;
}
return wb_replay_date_prefix + wb_orig_host + url;
}
// 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 WB_ExtractOrig(href) //============================================
{ function copy_object_fields(obj) {
if (!href) { var new_obj = {};
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)
{
var newLoc = WB_CopyObjectFields(loc);
newLoc._origLoc = loc;
newLoc._origHref = loc.href;
// Rewrite replace and assign functions
newLoc.replace = function(url) { this._origLoc.replace(WB_RewriteUrl(url)); }
newLoc.assign = function(url) { this._origLoc.assign(WB_RewriteUrl(url)); }
newLoc.reload = loc.reload;
// Adapted from:
// https://gist.github.com/jlong/2428561
var parser = document.createElement('a');
parser.href = WB_ExtractOrig(newLoc._origHref);
newLoc.hash = parser.hash; for (prop in obj) {
newLoc.host = parser.host; if ((typeof obj[prop]) != "function") {
newLoc.hostname = parser.hostname; new_obj[prop] = obj[prop];
newLoc.href = parser.href; }
}
if (newLoc.origin) { return new_obj;
newLoc.origin = parser.origin; }
}
newLoc.pathname = parser.pathname; //============================================
newLoc.port = parser.port function extract_orig(href) {
newLoc.protocol = parser.protocol; if (!href) {
newLoc.search = parser.search; return "";
}
newLoc.toString = function() { return this.href; } href = href.toString();
var index = href.indexOf("/http", 1);
return newLoc; if (index > 0) {
} return href.substr(index + 1);
} else {
return href;
}
}
function WB_wombat_updateLoc(reqHref, origHref, location) //============================================
{ function copy_location_obj(loc) {
if (reqHref && (WB_ExtractOrig(origHref) != WB_ExtractOrig(reqHref))) { var new_loc = copy_object_fields(loc);
var finalHref = WB_RewriteUrl(reqHref);
location.href = finalHref;
}
}
function WB_wombat_checkLocationChange(wbLoc, isTop) new_loc._orig_loc = loc;
{ new_loc._orig_href = loc.href;
var locType = (typeof wbLoc);
var location = (isTop ? window.top.location : window.location);
// String has been assigned to location, so assign it
if (locType == "string") {
WB_wombat_updateLoc(wbLoc, location.href, location)
} else if (locType == "object") {
WB_wombat_updateLoc(wbLoc.href, wbLoc._origHref, location);
}
}
var wombat_updating = false; // Rewrite replace and assign functions
new_loc.replace = function(url) {
this._orig_loc.replace(rewrite_url(url));
}
new_loc.assign = function(url) {
this._orig_loc.assign(rewrite_url(url));
}
new_loc.reload = loc.reload;
function WB_wombat_checkLocations() // Adapted from:
{ // https://gist.github.com/jlong/2428561
if (wombat_updating) { var parser = document.createElement('a');
return false; parser.href = extract_orig(new_loc._orig_href);
}
wombat_updating = true;
WB_wombat_checkLocationChange(window.WB_wombat_location, false);
if (window.self.location != window.top.location) {
WB_wombat_checkLocationChange(window.top.WB_wombat_location, true);
}
wombat_updating = false;
}
function WB_wombat_Init_SeededRandom(seed) new_loc.hash = parser.hash;
{ new_loc.host = parser.host;
// Adapted from: new_loc.hostname = parser.hostname;
// http://indiegamr.com/generate-repeatable-random-numbers-in-js/ new_loc.href = parser.href;
Math.seed = seed; if (new_loc.origin) {
function seededRandom() { new_loc.origin = parser.origin;
Math.seed = (Math.seed * 9301 + 49297) % 233280; }
var rnd = Math.seed / 233280;
return rnd; new_loc.pathname = parser.pathname;
} new_loc.port = parser.port
new_loc.protocol = parser.protocol;
new_loc.search = parser.search;
Math.random = seededRandom; new_loc.toString = function() {
} return this.href;
}
function WB_wombat_CopyHistoryFunc(history, funcName) return new_loc;
{ }
origFunc = history[funcName];
if (!origFunc) {
return;
}
history['_orig_' + funcName] = origFunc; //============================================
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);
function rewrittenFunc(stateObj, title, url) { location.href = final_href;
url = WB_RewriteUrl(url); }
return origFunc.call(history, stateObj, title, url); }
}
history[funcName] = rewrittenFunc; //============================================
function check_location_change(loc, is_top) {
var locType = (typeof loc);
return rewrittenFunc; var location = (is_top ? window.top.location : window.location);
}
function WB_wombat_Init_AjaxOverride() { // String has been assigned to location, so assign it
if (!window.XMLHttpRequest || !window.XMLHttpRequest.prototype || if (locType == "string") {
!window.XMLHttpRequest.prototype.open) { update_location(loc, location.href, location)
return;
}
var orig = window.XMLHttpRequest.prototype.open; } else if (locType == "object") {
update_location(loc.href, loc._orig_href, location);
}
}
function openRewritten(sMethod, sUrl, bAsync, sUser, sPassword) { //============================================
sUrl = WB_RewriteUrl(sUrl); function check_all_locations() {
return orig.call(this, sMethod, sUrl, bAsync, sUser, sPassword); if (wb_wombat_updating) {
} return false;
}
window.XMLHttpRequest.prototype.open = openRewritten; wb_wombat_updating = true;
}
function WB_wombat_Init(replayPrefix, captureDate, origHost, timestamp) check_location_change(window.WB_wombat_location, false);
{
WB_wombat_replayPrefix = replayPrefix;
WB_wombat_replayDatePrefix = replayPrefix + captureDate + "/";
WB_wombat_captureDatePart = "/" + captureDate + "/";
WB_wombat_origHost = "http://" + origHost;
// Location if (window.self.location != window.top.location) {
window.WB_wombat_location = WB_CopyLocationObj(window.self.location); check_location_change(window.top.WB_wombat_location, true);
document.WB_wombat_location = window.WB_wombat_location; }
wb_wombat_updating = false;
}
if (window.self.location != window.top.location) { //============================================
window.top.WB_wombat_location = WB_CopyLocationObj(window.top.location); function init_seeded_random(seed) {
} // Adapted from:
// http://indiegamr.com/generate-repeatable-random-numbers-in-js/
if (window.opener) { Math.seed = seed;
window.opener.WB_wombat_location = (window.opener ? WB_CopyLocationObj(window.opener.location) : null); function seeded_random() {
} Math.seed = (Math.seed * 9301 + 49297) % 233280;
var rnd = Math.seed / 233280;
// Domain return rnd;
document.WB_wombat_domain = origHost; }
// History Math.random = seeded_random;
WB_wombat_CopyHistoryFunc(window.history, 'pushState'); }
WB_wombat_CopyHistoryFunc(window.history, 'replaceState');
// Ajax //============================================
WB_wombat_Init_AjaxOverride(); function copy_history_func(history, func_name) {
orig_func = history[func_name];
// Random if (!orig_func) {
WB_wombat_Init_SeededRandom(timestamp); return;
} }
// Check quickly after page load history['_orig_' + func_name] = orig_func;
setTimeout(WB_wombat_checkLocations, 100);
function rewritten_func(state_obj, title, url) {
url = rewrite_url(url);
return orig_func.call(history, state_obj, title, url);
}
// Check periodically every few seconds history[func_name] = rewritten_func;
setInterval(WB_wombat_checkLocations, 500);
return rewritten_func;
}
//============================================
function init_ajax_rewrite() {
if (!window.XMLHttpRequest ||
!window.XMLHttpRequest.prototype ||
!window.XMLHttpRequest.prototype.open) {
return;
}
var orig = window.XMLHttpRequest.prototype.open;
function open_rewritten(method, url, async, user, password) {
url = rewrite_url(url);
return orig.call(this, method, url, async, user, password);
}
window.XMLHttpRequest.prototype.open = open_rewritten;
}
//============================================
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 + "/";
wb_orig_host = "http://" + orig_host;
// 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) {
window.top.WB_wombat_location = copy_location_obj(window.top.location);
}
if (window.opener) {
window.opener.WB_wombat_location = (window.opener ? copy_location_obj(window.opener.location)
: null);
}
// Domain
document.WB_wombat_domain = orig_host;
// History
copy_history_func(window.history, 'pushState');
copy_history_func(window.history, 'replaceState');
// Ajax
init_ajax_rewrite();
// Random
init_seeded_random(timestamp);
}
// Check quickly after page load
setTimeout(check_all_locations, 100);
// Check periodically every few seconds
setInterval(check_all_locations, 500);
return wombat_init;
})(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') }}");