var i18n = { t_d: "动车", t_g: "高铁", t_k: "普客", t_zt: "直达或特快" }; var seatType = { '9': "商务", "P": "特等", "M": "一等", "O": "二等", "6": "高软", "4": "软卧", "3": "硬卧", "2": "软座", "1": "硬座", "0": "无座" }; var seatTypeFull = { '9': "商务座", "P": "特等座", "M": "一等座", "O": "二等座", "6": "高软软卧", "4": "软卧", "3": "硬卧", "2": "软座", "1": "硬座", "0": "无座" }; var seatTypeDisplayOrder = "9PMO643210"; var sysConfig = null; var NONINFOBARMODE = location.search.length <= 4 || !parseInt(location.search.slice(4)); var LARGEICON = "/icons/icon_128.png"; var ISOTN = location.search.indexOf("?new") === 0; var evtTag = Math.random() + ''; function compressFunc() { return false; } $(function () { $("a[data-toggle=tooltip], label[data-toggle=tooltip], input[data-toggle=tooltip], button[data-toggle=tooltip]").bsTooltip(); $(document).popover({ selector: "[data-toggle=popover]" }); }); var log = new function () { var isDebug = (compressFunc + '').indexOf("false") != -1; this.print = function () { /// 向控制台打印消息 if (!isDebug) return; console.log.apply(console, $.makeArray(arguments)); }; }; var message = (function () { var messageProcessor = function () { var __ = this; var actionMap = {}; if (NONINFOBARMODE) { chrome.tabs.getCurrent(function (t) { __.tab = t; __.tabid = t.id; __.host = ISOTN ? "http://kyfw.12306.cn/" : "http://dynamic.12306.cn/"; $(document).trigger("tabidUpdate"); }); } else { __.tabid = parseInt(location.search.substr(4)); chrome.tabs.get(__.tabid, function (t) { __.tab = t; __.host = /(https?:\/\/[a-z]+\.12306.cn\/)/.exec(t.url) && RegExp.$1; ISOTN = __.tab.url.indexOf("/otn") !== -1; $(document).trigger("tabidUpdate"); }); } this.sendToTab = function (message, callback) { if (!__.tabid) return; message = $.extend(message, { tag: evtTag }); chrome.tabs.sendMessage(__.tabid, message, callback || function () { }); }; this.send = function (message, callback) { message = $.extend(message, { tag: evtTag }); chrome.runtime.sendMessage(message, callback || function () { }); }; this.sendActionToTab = function (action, detail, callback) { if (!__.tabid) return; chrome.tabs.sendMessage(__.tabid, { action: action, tag: evtTag, detail: detail }, callback || function () { }); }; this.sendAction = function (action, detail, callback) { chrome.runtime.sendMessage({ action: action, tag: evtTag, detail: detail }, callback || function () { }); }; this.addAction = function (name, callback) { var actionQueue = actionMap[name] = actionMap[name] || []; actionQueue.push(callback); }; chrome.runtime.onMessage.addListener(function (message, sender, response) { if ((sender.tab && sender.tab.id != __.tabid && sender.tab.url.indexOf("background.html") == -1) || !message || !message.action || (message.tag && message.tag === evtTag)) return; log.print(message); var action = message.action; var queue = actionMap[action]; if (!queue) return; $.each(queue, function () { this.call(message.detail, response, message, sender); }); }); }; //分发消息 var mp = new messageProcessor(); if (!NONINFOBARMODE) { mp.addAction("sysConfigUpdate", function () { log.print("NONINFOBAR mode, dispatch messages to tab."); mp.sendToTab({ action: "sysConfigUpdate", detail: this }); }); } return mp; })(); var notification = (function () { var notify = function () { var __ = this; var notifyMap = {}; this.mapParam = function (opts) { return { type: opts.type || "basic", title: opts.title || "提示", message: opts.message || "", iconUrl: opts.iconUrl || "/icons/icon_128.png", //contextMessage: opts.contextMessage || "", priority: opts.priority || 0, eventTime: opts.eventTime || null, buttons: _.map(opts.buttons, function (e) { return { title: e.title, iconUrl: e.iconUrl }; }) || null, imageUrl: opts.imageUrl || null, items: _.map(opts.items, function (e) { return { title: e.title, message: e.message }; }) || null//, //progress: opts.progress || null, //isClickable: opts.isClickable || true }; }; __.create = function (opts) { var opt = __.mapParam(opts); var id = opts.id || ""; chrome.notifications.create(id, opt, function (_id) { notifyMap[_id] = opts; opts.id = _id; }); opts.update = function () { __.update(opts); }; opts.clear = function () { __.clear(opts.id); }; }; __.update = function (opts) { var opt = __.mapParam(opts); chrome.notifications.update(opts.id, opt, function () { opts.onUpdate && opts.onUpdate.apply(opts, $.makeArray(arguments)); }); }; __.clear = function (id) { chrome.notifications.clear(id, function () { opts.onClear && opts.onClear.apply(opts, $.makeArray(arguments)); }); }; __.getAll = function (callback) { chrome.notifications.getAll(callback); }; __.getPermissionLevel = function (callback) { chrome.notifications.getPermissionLevel(callback); }; chrome.notifications.onClicked.addListener(function (id) { var opts = notifyMap[id]; if (opts === undefined || opts === null) return; if ($.isFunction(opts.onClick)) { opts.onClick.apply(this); } }); chrome.notifications.onClosed.addListener(function (id, byUser) { var opts = notifyMap[id]; if (opts === undefined || opts === null) return; if ($.isFunction(opts.onClose)) { opts.onClose.apply(this, [byUser]); } }); chrome.notifications.onButtonClicked.addListener(function (id, index) { var opts = notifyMap[id]; if (opts === undefined || opts === null) return; var button = opts.buttons && opts.buttons[index]; if (!button || !$.isFunction(button.onClick)) return; button.onClick.apply(opts); }); message.addAction("pageNotification", function () { __.create(this); }); message.addAction("pageNotify", function () { __.create(this); }); }; return new notify(); })(); (function initGlobalConfig() { var $doc = $(document); var suspendMessage = false; var refreshValue = function () { $("input[data-sysconfigkey], select[data-sysconfigkey]").each(function () { var ele = this; var $ele = $(this); var type = ele.type; var key = ele.dataset.sysconfigkey; var profile = sysConfig; var propertyvalue = profile[key]; if (typeof propertyvalue === 'boolean') { if (type === "radio") { $("input[name=" + ele.name + "][value=" + (propertyvalue ? 1 : 0) + "]")[0].checked = true; } else if (type === "checkbox") { ele.checked = propertyvalue; } else { $ele.val(propertyvalue ? "1" : "0"); } } else { $ele.val(propertyvalue); } }); }; $doc.on("change", "[data-sysconfigkey]", function () { var ele = this; var $ele = $(this); var type = ele.type; var key = ele.dataset.sysconfigkey; var profile = sysConfig; var propertyvalue = profile[key]; if (typeof propertyvalue === 'boolean') { if (type == "radio") { profile[key] = $("input[name=" + ele.name + "]:checked")[0].value == "1"; } else if (type === "checkbox") { profile[key] = ele.checked; } else { profile[key] = $ele.val() == "1"; } } else if (typeof propertyvalue === 'number') { profile[key] = parseInt($ele.val() + ''); } else { profile[key] = $.trim($ele.val()); } suspendMessage = true; log.print("SYSCONFIG changed by user action. sending message to notify clients."); chrome.runtime.sendMessage({ action: "sysConfigUpdate", detail: sysConfig }); suspendMessage = false; }); $(document).on("sysConfigUpdated", refreshValue); chrome.runtime.sendMessage({ action: "getSysConfig" }, function (message) { if (typeof message === 'undefined' || !message) { alert("警告!无法加载助手配置,请重新启动浏览器!"); } log.print("sysconfig updated."); sysConfig = message.detail; $doc.trigger("sysConfigUpdated"); }); message.addAction("sysConfigUpdate", function () { if (suspendMessage) return; sysConfig = this; $doc.trigger("sysConfigUpdated"); }); })();