Light12306/Web12306/js/boot.js

156 lines
3.6 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({
base: basePath,
alias: {
"jquery": "modules/jquery/jquery.js",
"underscore": "modules/underscore/underscore.js"
},
vars: {
'locale': 'zh-cn'
},
charset: "utf-8",
debug: true
});
//检测扩展
var notInstallExtension = function () {
2014-07-01 20:07:44 +08:00
seajs.use("ui/noextension");
2014-06-26 22:24:54 +08:00
};
2014-08-18 23:12:35 +08:00
window.targetExtensionId = null;
$(function() {
//确保内容脚本启动
var start = $.Deferred();
start.done(function () {
chrome.runtime.sendMessage(window.targetExtensionId, { action: "getStorage" }, function (m) {
window.storage = m.detail;
2014-08-18 23:12:35 +08:00
seajs.use("ui/" + pagename);
});
2014-08-18 23:12:35 +08:00
});
start.fail(function () {
notInstallExtension();
});
2014-08-18 23:12:35 +08:00
var checkExtensionInfo = function() {
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();
};
if (document.body.dataset["mobileSupportInitialized"]) {
checkExtensionInfo();
} else {
var timer = setTimeout(function () {
start.reject();
2014-08-30 01:32:27 +08:00
}, 1500);
2014-08-18 23:12:35 +08:00
document.addEventListener("mobileSupportInitialized", function () {
window.clearTimeout(timer);
checkExtensionInfo();
2014-06-26 22:24:54 +08:00
});
2014-08-18 23:12:35 +08:00
}
2014-08-13 00:14:00 +08:00
});
2014-05-16 20:10:45 +08:00
})(window, document);