(function(window) { var _instance = null; window.TallPlugin = function(config) { this.config = config; this.props = null; }; // 初始化并保证是单例 TallPlugin.init = function(config) { if (!_instance) { _instance = new TallPlugin(config); } return _instance; }; function postMsg(message) { let origin = '*'; window.postMessage(message, origin); } TallPlugin.prototype.created = function(callback) { console.log('created begin'); var _this = this; postMsg('created'); window.addEventListener( 'message', function({ data, origin }) { console.log('on created message, data, origin: ', data, origin); try { var target = JSON.parse(data); if (target.success) { _this.props = JSON.parse(data); callback && typeof callback === 'function' && callback.call(_this, _this.props); } // else { // _this.props = null; // } } catch (e) { _this.props = null; } }, false, ); // DOM加载完成 window.addEventListener('DOMContentLoaded', this.mounted, false); // window onload window.addEventListener('load', this.loaded, false); // destroy window.addEventListener('unload', this.destroy, false); // error window.addEventListener('error', this.error, false); return this; }; TallPlugin.prototype.mounted = function(callback) { console.log('mounted'); postMsg('mounted'); callback && typeof callback === 'function' && callback.call(this); return this; }; TallPlugin.prototype.loaded = function(callback) { console.log('loaded'); postMsg('loaded'); callback && typeof callback === 'function' && callback.call(this); return this; }; TallPlugin.prototype.destroy = function(callback) { console.log('destroy'); postMsg('destroy'); callback && typeof callback === 'function' && callback.call(this); return this; }; TallPlugin.prototype.error = function(callback) { console.log('error'); postMsg('error'); callback && typeof callback === 'function' && callback.call(this); return this; }; })(window);