Light12306/Web12306/js/boot.js

196 lines
4.8 KiB
JavaScript
Raw Normal View History

2014-05-16 20:10:45 +08:00
//12306入口引导脚本
(function (window, document) {
var basePath = window.location.protocol + "//" + window.location.host + "/js/";
var pagename = /\/(\w+?)\.html/.exec(location.pathname) && RegExp.$1 || "index";
var loadScript = function (path, callback) {
var script = document.createElement("script");
script.src = basePath + path;
script.onload = function () {
callback();
document.head.removeChild(script);
};
document.head.appendChild(script);
};
2014-06-26 22:24:54 +08:00
//extend jquery
$.fn.extend({
searchDelay: function (opts) {
var SearchDelay = function (e, opt) {
var __ = this;
this.$element = e;
this.timer = null;
this.opt = $.extend(SearchDelay.defaults, opt);
this.$element.keyup(function () {
__.reset();
__.timer = setTimeout(function () {
__.search.apply(__);
}, __.opt.delay);
});
};
SearchDelay.defaults = {
delay: 300
};
SearchDelay.prototype.search = function () {
this.$element.trigger("search", this.$element.val());
};
SearchDelay.prototype.reset = function () {
if (this.timer)
clearTimeout(this.timer);
};
SearchDelay.prototype.cancel = function () {
this.reset();
this.$element.val("");
this.search();
};
this.each(function () {
var key = "fish.searchdelay";
var $ele = $(this);
var target = $ele.data(key);
if (!target) {
target = new SearchDelay($ele, opts);
$ele.data(key, target);
}
if (typeof (opts) === "string") {
target[opts].apply(target, $.makeArray(arguments).slice(1));
}
});
return this;
}
}
);
2014-07-29 21:19:06 +08:00
//fix basic page action
$(document).on("focus", "input[placeholder]", function () {
this.dataset.placeholder = this.placeholder;
this.placeholder = "";
}).on("blur", "input", function () {
if (!this.dataset.placeholder)
return;
this.placeholder = this.dataset.placeholder;
this.dataset.placeholder = null;
});
2014-06-26 22:24:54 +08:00
2014-05-16 20:10:45 +08:00
//extend underscore
_.mixin({
mapObject: function (array, keySelector) {
var obj = {};
_.each(array, function (e) {
obj[keySelector(e)] = e;
});
return obj;
2014-07-29 21:19:06 +08:00
},
removeFromArray: function (array, obj) {
if (!array)
return;
var idx = _.indexOf(array, obj);
if (idx !== -1)
array.splice(idx, 1);
2014-05-16 20:10:45 +08:00
}
});
2014-06-26 22:24:54 +08:00
seajs.config({
2015-02-08 23:31:05 +08:00
base: basePath
2014-06-26 22:24:54 +08:00
});
//检测扩展
var notInstallExtension = function () {
2014-07-01 20:07:44 +08:00
seajs.use("ui/noextension");
2014-06-26 22:24:54 +08:00
};
var extensionDisabled = function () {
seajs.use("ui/extesniondisabled");
};
2014-08-18 23:12:35 +08:00
window.targetExtensionId = null;
2014-09-05 18:34:16 +08:00
$(function () {
2014-08-18 23:12:35 +08:00
//确保内容脚本启动
var start = $.Deferred();
start.done(function () {
chrome.runtime.sendMessage(window.targetExtensionId, { action: "getStorage" }, function (m) {
window.storage = m.detail;
//TODO: 因为压缩需要mark所有的页面名所以不可以用变量计算。
if (pagename === "index")
seajs.use("ui/index");
2014-09-05 18:34:16 +08:00
});
2014-08-18 23:12:35 +08:00
});
start.fail(function (reason) {
if (reason === "disabled")
extensionDisabled();
else
notInstallExtension();
2014-08-18 23:12:35 +08:00
});
2014-09-05 18:34:16 +08:00
start.always(function () {
var page = location.pathname;
if (location.search) {
page += location.search + "&";
2014-09-05 18:34:16 +08:00
} else {
page += "?";
}
2014-09-10 21:49:46 +08:00
page += "bv=" + (document.body.dataset["browserVersion"]||"") + "&ev=" + (document.body.dataset["targetExtensionVersion"]||"");
2014-09-05 18:34:16 +08:00
//加入百度统计
window._hmt = window._hmt || [];
_hmt.push(['_setAutoPageview', false]);
_hmt.push(['_trackPageview', page]);
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?c7150a056b54f666f90c1a05e0700798";
document.getElementsByTagName("head")[0].appendChild(hm);
});
2014-09-05 18:34:16 +08:00
var checkExtensionInfo = function () {
2014-08-18 23:12:35 +08:00
window.targetExtensionId = document.body.dataset["targetExtensionId"];
window.targetExtensionVersion = document.body.dataset["targetExtensionVersion"];
if (!window.targetExtensionId) {
start.reject();
return;
}
2014-08-18 23:12:35 +08:00
//检测版本吗?暂时不需要
//完成!
start.resolve();
};
2014-09-10 21:49:46 +08:00
var timeout = 5000;
2014-08-18 23:12:35 +08:00
var waitForStart = function () {
if (document.body.dataset["mobileSupportInitialized"]) {
2014-08-18 23:12:35 +08:00
checkExtensionInfo();
} else {
var timer = setTimeout(function () {
start.reject();
}, timeout);
document.addEventListener("mobileSupportInitialized", function () {
window.clearTimeout(timer);
checkExtensionInfo();
});
}
};
2014-09-10 21:49:46 +08:00
//var extJson = null;
//if (window.external.GetExtensionInfoListJson && (extJson = window.external.GetExtensionInfoListJson())) {
// var ext = _.findWhere(JSON.parse(extJson), { id: "gkbheeokbgmmnbjhhlphckobccejghjn" });
// if (ext) {
// if (!ext.enabled)
// start.reject("disabled");
// else {
// timeout = 10000;
// waitForStart();
// }
// } else {
// waitForStart();
// }
//} else {
// waitForStart();
//}
waitForStart();
2014-08-13 00:14:00 +08:00
});
2014-05-16 20:10:45 +08:00
})(window, document);
2014-09-05 18:34:16 +08:00