Light12306/Web12306/js/boot.js
2014-08-13 00:14:00 +08:00

168 lines
3.9 KiB
JavaScript

//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);
};
//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;
}
}
);
//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;
});
//extend underscore
_.mixin({
mapObject: function (array, keySelector) {
var obj = {};
_.each(array, function (e) {
obj[keySelector(e)] = e;
});
return obj;
},
removeFromArray: function (array, obj) {
if (!array)
return;
var idx = _.indexOf(array, obj);
if (idx !== -1)
array.splice(idx, 1);
}
});
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 () {
seajs.use("ui/noextension");
};
var entryPoint = function () {
if (!window.targetExtensionId) {
notInstallExtension();
} else {
//确保内容脚本启动
var start = $.Deferred();
start.done(function () {
chrome.runtime.sendMessage(window.targetExtensionId, { action: "getStorage" }, function (m) {
window.storage = m.detail;
seajs.use("ui/" + pagename);
});
});
start.fail(function () {
notInstallExtension();
});
if (document.body.dataset["mobileSupportInitialized"]) {
start.resolve();
} else {
var timer = setTimeout(function () {
start.reject();
}, 3000);
document.addEventListener("mobileSupportInitialized", function () {
window.clearTimeout(timer);
start.resolve();
});
}
}
};
var targetExtension = [
"bpbefagpafkfgoihbmcgeileodldkpnf",
"gkbheeokbgmmnbjhhlphckobccejghjn"
];
window.targetExtensionId = null;
var nextTest = function () {
if (targetExtension.length) {
var id = targetExtension.pop();
chrome.runtime.sendMessage(id, { action: "getStorage" }, function (m) {
if (m) {
window.targetExtensionId = id;
window.storage = m.detail;
entryPoint();
} else {
nextTest();
}
});
} else entryPoint();
};
$(function() {
if (typeof (chrome) !== 'undefined') {
nextTest();
} else entryPoint();
});
})(window, document);