2014-06-12 21:36:05 +08:00
|
|
|
|
define(function (require, exports, module) {
|
2014-09-05 18:34:16 +08:00
|
|
|
|
var param = require("../data.js");
|
2014-06-26 22:24:54 +08:00
|
|
|
|
exports.targetId = window.targetExtensionId;
|
2014-06-12 21:36:05 +08:00
|
|
|
|
|
2014-08-08 20:46:37 +08:00
|
|
|
|
var EventObject = require("./EventObject.js");
|
2014-07-23 17:33:09 +08:00
|
|
|
|
|
2014-08-08 20:46:37 +08:00
|
|
|
|
var Port = function () {
|
|
|
|
|
EventObject.apply(this);
|
|
|
|
|
var port = chrome.runtime.connect(exports.targetId, { name: "12306Chat" });
|
|
|
|
|
var that = this;
|
|
|
|
|
var connected = true;
|
2014-07-23 17:33:09 +08:00
|
|
|
|
|
2014-08-08 20:46:37 +08:00
|
|
|
|
port.onMessage.addListener(function (m) {
|
|
|
|
|
if (!m || !m.action)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
that.fireEvent(m.action, m.detail || null);
|
|
|
|
|
});
|
2014-08-14 21:33:47 +08:00
|
|
|
|
port.onDisconnect.addListener(function () {
|
2014-08-08 20:46:37 +08:00
|
|
|
|
that.fireEvent("disconnect");
|
2014-08-14 21:33:47 +08:00
|
|
|
|
});
|
2014-08-08 20:46:37 +08:00
|
|
|
|
this.postMessage = function (action, detail) {
|
|
|
|
|
port.postMessage({
|
|
|
|
|
action: action,
|
|
|
|
|
detail: detail || null
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Object.defineProperty(this, "connected", {
|
|
|
|
|
get: function () {
|
|
|
|
|
return connected;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
Port.prototype = Object.create(EventObject);
|
|
|
|
|
Port.constructor = Port;
|
|
|
|
|
|
|
|
|
|
var _port;
|
|
|
|
|
Object.defineProperty(exports, "port", {
|
|
|
|
|
get: function () {
|
|
|
|
|
return _port || (_port = new Port());
|
|
|
|
|
}
|
|
|
|
|
});
|
2014-07-23 17:33:09 +08:00
|
|
|
|
|
|
|
|
|
exports.postMessage = function (message) {
|
2014-08-26 21:29:58 +08:00
|
|
|
|
exports.port.postMessage(message);
|
2014-07-23 17:33:09 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.sendMessage = function (m, response) {
|
2014-06-12 21:36:05 +08:00
|
|
|
|
if (!exports.targetId)
|
|
|
|
|
throw "extension not connected.";
|
|
|
|
|
|
|
|
|
|
chrome.runtime.sendMessage(exports.targetId, m, response || function () { });
|
|
|
|
|
};
|
2014-08-08 20:46:37 +08:00
|
|
|
|
|
|
|
|
|
exports.track = function (type, values) {
|
|
|
|
|
/// <summary>发出跟踪记录</summary>
|
|
|
|
|
chrome.runtime.sendMessage(exports.targetId, {
|
|
|
|
|
action: "track",
|
|
|
|
|
detail: {
|
|
|
|
|
type: type,
|
|
|
|
|
values: values
|
|
|
|
|
}
|
|
|
|
|
}, function () { });
|
2015-02-08 23:31:05 +08:00
|
|
|
|
|
|
|
|
|
//百度统计
|
2014-09-05 18:34:16 +08:00
|
|
|
|
if (window._hmt && type !== param.trackTypes.OPEN_PAGE_INDEX) {
|
2015-02-08 23:31:05 +08:00
|
|
|
|
try {
|
|
|
|
|
var data = "";
|
|
|
|
|
if (values) {
|
|
|
|
|
data = values.map(function(s) {
|
|
|
|
|
return encodeURIComponent(s);
|
|
|
|
|
}).join("&");
|
|
|
|
|
}
|
|
|
|
|
window._hmt.push(['_trackEvent', '12306', type, data]);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-08 20:46:37 +08:00
|
|
|
|
};
|
2014-09-01 20:24:57 +08:00
|
|
|
|
|
|
|
|
|
window.port = exports;
|
2014-06-27 22:25:42 +08:00
|
|
|
|
});
|