212 lines
5.3 KiB
JavaScript
212 lines
5.3 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);
|
||
},
|
||
sortArray: function (array, comparer) {
|
||
//保存原始排序
|
||
var map = {};
|
||
for (var i = 0; i < array.length; i++) {
|
||
map[array[i]] = array[i];
|
||
}
|
||
array.sort(function (x, y) {
|
||
var result = comparer && comparer(x, y);
|
||
|
||
return result === 0 ? map[x] - map[y] : result;
|
||
});
|
||
}
|
||
});
|
||
seajs.config({
|
||
base: basePath
|
||
});
|
||
|
||
//检测扩展
|
||
var notInstallExtension = function () {
|
||
seajs.use("ui/noextension");
|
||
};
|
||
var extensionDisabled = function () {
|
||
seajs.use("ui/extesniondisabled");
|
||
};
|
||
window.targetExtensionId = null;
|
||
if ('ontouchstart' in window) {
|
||
self.location.replace("http://12306.m.liebao.cn/?f=pc");
|
||
} else {
|
||
$(function () {
|
||
//确保内容脚本启动
|
||
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");
|
||
|
||
});
|
||
});
|
||
start.fail(function (reason) {
|
||
if (reason === "disabled")
|
||
extensionDisabled();
|
||
else
|
||
notInstallExtension();
|
||
});
|
||
start.always(function () {
|
||
var page = location.pathname;
|
||
if (location.search) {
|
||
page += location.search + "&";
|
||
} else {
|
||
page += "?";
|
||
}
|
||
page += "bv=" + (document.body.dataset["browserVersion"] || "") + "&ev=" + (document.body.dataset["targetExtensionVersion"] || "");
|
||
//加入百度统计
|
||
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);
|
||
});
|
||
|
||
var checkExtensionInfo = function () {
|
||
window.targetExtensionId = document.body.dataset["targetExtensionId"];
|
||
window.targetExtensionVersion = document.body.dataset["targetExtensionVersion"];
|
||
|
||
if (!window.targetExtensionId) {
|
||
start.reject();
|
||
return;
|
||
}
|
||
//检测版本吗?暂时不需要
|
||
|
||
|
||
//完成!
|
||
start.resolve();
|
||
};
|
||
var timeout = 5000;
|
||
|
||
var waitForStart = function () {
|
||
if (document.body.dataset["mobileSupportInitialized"]) {
|
||
checkExtensionInfo();
|
||
} else {
|
||
var timer = setTimeout(function () {
|
||
start.reject();
|
||
}, timeout);
|
||
document.addEventListener("mobileSupportInitialized", function () {
|
||
window.clearTimeout(timer);
|
||
checkExtensionInfo();
|
||
});
|
||
}
|
||
};
|
||
|
||
//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();
|
||
});
|
||
}
|
||
})(window, document);
|
||
|
||
|