Skip to content
This repository
Browse code

updatev1.1

  • Loading branch information...
commit ecb541bda9cd3ab1f50b676b5bfb1cd57f0a0325 1 parent 3e54097
司徒正美 authored

Showing 2 changed files with 127 additions and 151 deletions. Show diff stats Hide diff stats

  1. +127 5 index.html
  2. +0 146 messenger.js
132 index.html
@@ -3,7 +3,12 @@
3 3 <html>
4 4 <head>
5 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
6   - <script src="http://files.cnblogs.com/rubylouvre/mass_merge.js"></script>
  6 + <script src="mass_merge.js"></script>
  7 + <style>
  8 + #ccc {
  9 + border:1px solid red;
  10 + }
  11 + </style>
7 12 <script>
8 13 require("messenger,event,ready", function(Messenger, $) {
9 14 var iframe = $("#iframe");
@@ -11,23 +16,140 @@
11 16 target: '#iframe',
12 17 onmessage: function(data) {
13 18 $.log(data, true)
14   - if(typeof data == "object"){
15   - $.log(data.aaa)
  19 + if (typeof data == "object") {
  20 + $.log(data.aaa, true);
  21 + $.log(data.fn, true);
16 22 }
17 23 }
18 24 });
19 25 iframe.on("load", function() {
20 26 messager.send('发给子页面的消息');
21 27 });
  28 + var el = $("#aaa")[0]
  29 + var div = document.createElement("div");
  30 + div.id = "ccc"
  31 + el.applyElement(div,"inside")
22 32
23 33 })
  34 +(function (constructors, defineGetter, defineSetter) {
  35 + constructors.forEach (defineGetter);
  36 + constructors.forEach (defineSetter);
  37 +})(
  38 + [ Element, Attr, EntityReference, Entity, DocumentFragment ],
  39 +
  40 + (function (getInnerText) {
  41 + return function (C) { C.prototype.__defineGetter__('innerText', getInnerText); };
  42 + })(
  43 + (function (getInnerText) {
  44 + return function () { return Array.map (this.childNodes, getInnerText).join (''); };
  45 + })(
  46 + function (n) { return n.innerText; })),
  47 +
  48 + (function (setInnerText) {
  49 + return function (C) { C.prototype.__defineSetter__('innerText', setInnerText); };
  50 + })(
  51 + function (s) {
  52 + while (this.hasChildNodes ()) this.removeChild (this.firstChild);
  53 + this.appendChild (this.ownerDocument.createTextNode (s));
  54 + }));
  55 +
  56 +(function (constructors, defineGetter, defineSetter) {
  57 + constructors.forEach (defineGetter);
  58 + constructors.forEach (defineSetter);
  59 +})(
  60 + [ Text, CDATASection ],
  61 +
  62 + (function (getInnerText) {
  63 + return function (C) { C.prototype.__defineGetter__('innerText', getInnerText); };
  64 + })(
  65 + function () { return this.data; }),
  66 +
  67 + (function (setInnerText) {
  68 + return function (C) { C.prototype.__defineSetter__('innerText', setInnerText); };
  69 + })(
  70 + function (s) { this.data = s; }));
  71 +
  72 +(function (constructors, defineGetter, defineSetter) {
  73 + constructors.forEach (defineGetter);
  74 + constructors.forEach (defineSetter);
  75 +})(
  76 + [ ProcessingInstruction, Comment, Document, DocumentType, Notation ],
  77 +
  78 + (function (getInnerText) {
  79 + return function (C) { C.prototype.__defineGetter__('innerText', getInnerText); };
  80 + })(
  81 + function () { return ''; }),
  82 +
  83 + (function (setInnerText) {
  84 + return function (C) { C.prototype.__defineSetter__('innerText', setInnerText); };
  85 + })(
  86 + function (s) { throw new TypeError ('DOMException.NO_MODIFICATION_ALLOWED_ERR(7)'); }));
  87 +HTMLElement.prototype.__defineGetter__('innerText', function () {
  88 + var s = [ ];
  89 + var d = this.ownerDocument || this;
  90 + var t = NodeFilter.SHOW_TEXT | NodeFilter.SHOW_CDATA_SECTION;
  91 + var w = d.createTreeWalker (this, t, null, true);
  92 + var n;
  93 + while (n = w.nextNode ()) s[s.length] = n.data;
  94 + return s.join ('');
  95 +});
  96 +HTMLElement.prototype.__defineGetter__('innerText', function () {
  97 + var d = this.ownerDocument || this;
  98 + var r = d.createRange ();
  99 + r.selectNodeContents (this);
  100 + return r.toString ();
  101 +});
  102 +HTMLElement.prototype.__defineGetter__('innerText', function () {
  103 + var d = this.ownerDocument || this;
  104 + var x = 'string(self::node())';
  105 + var r = d.createNSResolver (this);
  106 + var t = XPathResult.STRING_TYPE;
  107 + return d.evaluate (x, this, r, t, null).stringValue;
  108 +});
  109 +// <http://www.w3.org/TR/ElementTraversal/>
  110 +
  111 +(function (proto, getElement) {
  112 + proto.__defineGetter__ ('firstElementChild', function () {
  113 + return getElement.call (this, 'firstChild', 'nextSibling');
  114 + });
  115 +
  116 + proto.__defineGetter__ ('lastElementChild', function () {
  117 + return getElement.call (this, 'lastChild', 'previousSibling');
  118 + });
  119 +
  120 + proto.__defineGetter__ ('previousElementSibling', function () {
  121 + return getElement.call (this, 'previousSibling');
  122 + });
  123 +
  124 + proto.__defineGetter__ ('nextElementSibling', function () {
  125 + return getElement.call (this, 'nextSibling');
  126 + });
  127 +
  128 + proto.__defineGetter__ ('childElementCount', function () {
  129 + for (var count = 0, n = this.firstElementChild; n; n = n.nextElementSibling, count++);
  130 + return count;
  131 + });
  132 +} )(
  133 + Element.prototype,
  134 +
  135 + function (start, direction) {
  136 + for (var node = this[start]; node; node = node[direction || start]) {
  137 + if (node.nodeType == Node.ELEMENT_NODE) {
  138 + break;
  139 + }
  140 + }
  141 + return node;
  142 + }
  143 +);
  144 +
  145 +
24 146
25 147 </script>
26 148
27 149 </head>
28 150 <body>
29   - <p>mass messager 组件 by 司徒正美</p>
30   - <iframe id="iframe" src="http://www.cnblogs.com/rubylouvre/articles/2957674.html">
  151 + <p id="aaa">mass messager 组件 by 司徒正美</p>
  152 + <iframe id="iframe" width="800" height="200" src="iframe.html">
31 153 </iframe>
32 154
33 155 </body>
146 messenger.js
... ... @@ -1,146 +0,0 @@
1   -define(["node"], function($) {
2   - function Messenger(config) {
3   - //win为其他页面的window对象或装载此window对象的iframe元素或其表达式
4   - var win = config.target
5   - if (typeof win === "string") {
6   - win = $(win).get(0);
7   - if (win && win.tagName === 'IFRAME') {
8   - win = win.contentWindow;
9   - }
10   - } else {
11   - win = parent;
12   - }
13   - this.win = win;
14   - this._messages = [];
15   - //onmessage为当前页面处理其他页面发过来的
16   - if (typeof config.onmessage === "function") {
17   - this.receive(config.onmessage);
18   - }
19   - this.init();
20   - }
21   -
22   - Messenger.prototype = {
23   - init: function() {
24   - var me = this;
25   - me._callback = function(event) {
26   - if (event.source != me.win)
27   - return;//如果不是来源自win所指向的窗口,返回
28   - var data = event.data;
29   - if (me._hack && data.indexOf(me.hack) === 0) {
30   - data = data.replace(me._hack, "");
31   - data = JSON.parse(data, function(k, v) {
32   - if (v.indexOf && v.indexOf('function') > -1) {
33   - return eval("(function(){return " + v + " })()")
34   - }
35   - return v;
36   - });
37   - }
38   - for (var i = 0, fn; fn = me._messages[i++]; ) {
39   - fn.call(me, data);
40   - }
41   - };
42   - var mode = document.documentMode;
43   - if (mode === 8 || mode === 9) {
44   - this._hack = String(new Date - 0);
45   - }
46   - $.bind(window, "message", me._callback);
47   - },
48   - receive: function(fn) {
49   - fn.win = this.win;
50   - this._messages.push(fn);
51   - },
52   - send: function(data) {
53   - if (this._hack && data && typeof data === "object") {
54   - data = JSON.stringify(data, function(key, val) {
55   - if (typeof val === 'function') {
56   - return val + '';
57   - }
58   - return val;
59   - });
60   - data = this._hack + data
61   - }
62   - this.win.postMessage(data, '*');//parent
63   - }
64   - };
65   - if (!"1"[0]) {//IE6-7
66   - Messenger.prototype.init = function() {
67   - var isSameOrigin = false;
68   - //判定是否同源,不同源会无法访问它的属性抛错
69   - try {
70   - isSameOrigin = !!this.win.location.href;
71   - } catch (e) {
72   - }
73   - if (isSameOrigin) {
74   - this.initForSameOrigin();
75   - } else {
76   - this.initForCrossDomain();
77   - }
78   -
79   - };
80   -
81   - Messenger.prototype.initForCrossDomain = function() {
82   - var fns = navigator.messages = navigator.messages || [];
83   - var me = this;
84   - for (var i = 0, fn; fn = this._messages[i++]; ) {
85   - fns.push(fn);
86   - }
87   - this.receive = function(fn) {
88   - fn.win = this.win;
89   - fns.push(fn);
90   - };
91   - this.send = function(data) {
92   - setTimeout(function() {
93   - for (var i = 0, fn; fn = fns[i++]; ) {
94   - if (fn.win != me.win) {
95   - fn.call(me, data);
96   - }
97   - }
98   - });
99   - };
100   - }
101   -
102   - Messenger.prototype.initForSameOrigin = function() {
103   - var me = this;
104   - this.send = function(data) {
105   - setTimeout(function() {
106   - var event = me.win.document.createEventObject();
107   - event.eventType = 'message';
108   - event.eventSource = window;
109   - event.eventData = data;
110   - me.win.document.fireEvent('ondataavailable', event);
111   - });
112   - }
113   - this._dataavailable = function(event) {
114   - if (event.eventType !== 'message' || event.eventSource != me.win)
115   - return;
116   - for (var i = 0, fn; fn = me._messages[i++]; ) {
117   - fn.call(me, event.eventData);
118   - }
119   - };
120   - document.attachEvent('ondataavailable', this._dataavailable);
121   - };
122   - }
123   -
124   - Messenger.prototype.destroy = function() {
125   - // 解除绑定事件
126   - if (this._callback) {
127   - $.unbind(this.win, "message", this._callback)
128   - }
129   - // 解除绑定事件 ie
130   - if (document.detachEvent && this._dataavailable) {
131   - document.detachEvent('ondataavailable', this._dataavailable);
132   - }
133   - // 删除实例属性
134   - for (var p in this) {
135   - if (this.hasOwnProperty(p)) {
136   - delete this[p];
137   - }
138   - }
139   - navigator.messages = void 0;
140   - };
141   -
142   - return Messenger;
143   -});
144   -//如果想在旧式的标准浏览器支持跨域通信,可以使用window.name;
145   -//https://github.com/biqing/MessengerJS/blob/master/messenger.js
146   -//https://github.com/aralejs/messenger/blob/master/src/messenger.js

0 comments on commit ecb541b

Please sign in to comment.
Something went wrong with that request. Please try again.