var base64 = base64 || (function () {
var base64Map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split('');
var base64DeMap = !function () {
var result = {};
$.each(base64Map, function (i, e) {
result[e] = i;
});
return result;
}();
var target = {};
target.encode = function (data) {
var buf = [];
var map = base64Map;
var n = data.length; //总字节数
var val; //中间值
var i = 0;
/*
* 3字节 ==> val ==> 4字符
*/
while (i < n) {
val = (data[i] << 16) |
(data[i + 1] << 8) |
(data[i + 2]);
buf.push(map[val >> 18],
map[val >> 12 & 63],
map[val >> 6 & 63],
map[val & 63]);
i += 3;
}
if (n % 3 == 1) //凑两个"="
buf.pop(), buf.pop(), buf.push('=', '=');
else //凑一个"="
buf.pop(), buf.push('=');
return buf.join('');
};
target.decode = function (str) {
var buf = [];
var arr = str.split('');
var map = base64DeMap;
var n = arr.length; //总字符数
var val; //中间值
var i = 0;
/*
* 长度异常
*/
if (n % 4)
return null;
/*
* 4字符 ==> val ==> 3字节
*/
while (i < n) {
val = (map[arr[i]] << 18) |
(map[arr[i + 1]] << 12) |
(map[arr[i + 2]] << 6) |
(map[arr[i + 3]]);
buf.push(val >> 16,
val >> 8 & 0xFF,
val & 0xFF);
i += 4;
}
/*
* 凑字字符"="个数
*/
while (arr[--n] == '=')
buf.pop();
return buf;
};
target.encodeArrayBuffer = function (arrayBuffer) {
/// 将ArrayBuffer转换为Base64字符串
var dv = new DataView(arrayBuffer);
var length = dv.byteLength;
var data = [];
for (var i = 0; i < length; i++) {
data.push(dv.getUint8(i));
}
return base64.encode(data);
};
target.toObjectUrl = function (base64str, type) {
/// 获得指定base64字符串的对象链接格式
return "data:" + type + ";base64," + base64str;
};
return target;
})();
$(function () {
//监听ajax事件
document.addEventListener("ajaxproxy", function (e) {
var detail = e.detail;
var data = detail.data;
var handle = {
success: null,
status: null,
statusText: null,
url: data.url,
text: null,
model: null,
request: data,
index: detail.index
};
//set header
data.headers = {
"Fish-Referer": data.refer || null,
"Fish-User-Agent": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Fish-Origin": /(https?:\/\/[^/]+\/)/i.exec(data.url)[1]
};
$.ajax(data).done(function (result, status, xhr) {
handle.success = true;
handle.status = xhr.status;
handle.statusText = xhr.statusText;
handle.text = xhr.responseText;
handle.model = result;
notifyAjaxComplete(handle);
}).fail(function (xhr) {
handle.success = false;
handle.status = xhr.status;
handle.statusText = xhr.statusText || "";
handle.text = xhr.responseText || "";
handle.model = null;
notifyAjaxComplete(handle);
});
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
});
var notifyAjaxComplete = function (key) {
var e = new CustomEvent("ajaxproxyfinished", { detail: key });
document.dispatchEvent(e);
};
document.addEventListener("ajaxLoadVerifyCode", function (e) {
var ctx = e.detail;
var xhr = new XMLHttpRequest();
xhr.open("GET", ctx.url, true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status != 200) {
document.dispatchEvent(new CustomEvent("ajaxproxyfinished", { detail: { url: null, success: false, index: ctx.index } }));
} else {
var imgUrl = base64.toObjectUrl(base64.encodeArrayBuffer(xhr.response), "image/jpeg");
document.dispatchEvent(new CustomEvent("ajaxproxyfinished", { detail: { url: imgUrl, success: true, index: ctx.index } }));
}
}
};
xhr.responseType = "arraybuffer";
xhr.setRequestHeader("Fish-Referer", ctx.refer);
xhr.setRequestHeader("Fish-User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");
xhr.setRequestHeader("Fish-Origin", /(https?:\/\/[^/]+\/)/.exec(ctx.url)[1]);
xhr.send(null);
e.preventDefault && e.preventDefault();
e.stopPropagation && e.stopPropagation();
});
document.body.dataset["targetExtensionId"] = chrome.runtime.id;
document.body.dataset["targetExtensionVersion"] = chrome.runtime.getManifest().version;
document.body.dataset["mobileSupportInitialized"] = 1;
document.dispatchEvent(new CustomEvent("mobileSupportInitialized"));
});