From cc3ed31860ac1a14eb9d64661b67f705a166a0bb Mon Sep 17 00:00:00 2001 From: Junle Li Date: Fri, 9 May 2014 02:05:28 +0800 Subject: [PATCH 1/6] Add jQuery postMessage Typescript definition file. --- jquery.postMessage/jquery.postMessage.d.ts | 44 ++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 jquery.postMessage/jquery.postMessage.d.ts diff --git a/jquery.postMessage/jquery.postMessage.d.ts b/jquery.postMessage/jquery.postMessage.d.ts new file mode 100644 index 000000000..b2c0ac124 --- /dev/null +++ b/jquery.postMessage/jquery.postMessage.d.ts @@ -0,0 +1,44 @@ +// Type definitions for jQuery postMessage +// Project: http://benalman.com/projects/jquery-postmessage-plugin/ +// Definitions by: Junle Li +// Official docs: http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html +// License: MIT license http://mit-license.org/ + +interface JQueryStatic { + + /** + * This method will call window.postMessage if available, setting the targetOrigin parameter to the base of the targetUrl parameter for maximum security in browsers that support it. If window.postMessage is not available, the target window’s location.hash will be used to pass the message. If an object is passed as the message param, it will be serialized into a string using the jQuery.param method. + * + * @param message A message to be passed to the other frame. + * @param targetUrl The URL of the other frame this window is attempting to communicate with. This must be the exact URL (including any query string) of the other window for this script to work in browsers that don’t support window.postMessage. + * @param target A reference to the other frame this window is attempting to communicate with. If omitted, defaults to `parent`. + */ + postMessage(message: string, targetUrl: string, target?: Window): void; + + /** + * This method will call window.postMessage if available, setting the targetOrigin parameter to the base of the targetUrl parameter for maximum security in browsers that support it. If window.postMessage is not available, the target window’s location.hash will be used to pass the message. If an object is passed as the message param, it will be serialized into a string using the jQuery.param method. + * + * @param message An object to be serialized into a params string, using the jQuery.param method. + * @param targetUrl The URL of the other frame this window is attempting to communicate with. This must be the exact URL (including any query string) of the other window for this script to work in browsers that don’t support window.postMessage. + * @param target A reference to the other frame this window is attempting to communicate with. If omitted, defaults to `parent`. + */ + postMessage(message: { [key: string]: any; }, targetUrl: string, target?: Window): void; + + /** + * Register a single callback for either a window.postMessage call, if supported, or if unsupported, for any change in the current window location.hash. If window.postMessage is supported and sourceOrigin is specified, the source window will be checked against this for maximum security. If window.postMessage is unsupported, a polling loop will be started to watch for changes to the location.hash. + * + * @param callback This callback will execute whenever a jQuery.postMessage message is received, provided the sourceOrigin matches. If callback is omitted, any existing receiveMessage event bind or polling loop will be canceled. + * @param sourceOrigin If window.postMessage is available and this value is not equal to the event.origin property, the callback will not be called. + * @param delay An optional zero-or-greater delay in milliseconds at which the polling loop will execute (for browser that don’t support window.postMessage). If omitted, defaults to 100. + */ + receiveMessage(callback: (event: MessageEvent) => any, sourceOrigin?: string, delay?: number): void; + + /** + * Register a single callback for either a window.postMessage call, if supported, or if unsupported, for any change in the current window location.hash. If window.postMessage is supported and sourceOrigin is specified, the source window will be checked against this for maximum security. If window.postMessage is unsupported, a polling loop will be started to watch for changes to the location.hash. + * + * @param callback This callback will execute whenever a jQuery.postMessage message is received, provided the sourceOrigin matches. If callback is omitted, any existing receiveMessage event bind or polling loop will be canceled. + * @param sourceOrigin If window.postMessage is available and this function returns false when passed the event.origin property, the callback will not be called. + * @param delay An optional zero-or-greater delay in milliseconds at which the polling loop will execute (for browser that don’t support window.postMessage). If omitted, defaults to 100. + */ + receiveMessage(callback: (event: MessageEvent) => any, sourceOrigin?: (origin: string) => boolean, delay?: number): void; +} \ No newline at end of file From a2302c15fa3ddbf82f09820589b7c55217c93995 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Fri, 9 May 2014 02:07:34 +0800 Subject: [PATCH 2/6] Add new line at end of the file. --- jquery.postMessage/jquery.postMessage.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.postMessage/jquery.postMessage.d.ts b/jquery.postMessage/jquery.postMessage.d.ts index b2c0ac124..2ac58ce91 100644 --- a/jquery.postMessage/jquery.postMessage.d.ts +++ b/jquery.postMessage/jquery.postMessage.d.ts @@ -41,4 +41,4 @@ interface JQueryStatic { * @param delay An optional zero-or-greater delay in milliseconds at which the polling loop will execute (for browser that don’t support window.postMessage). If omitted, defaults to 100. */ receiveMessage(callback: (event: MessageEvent) => any, sourceOrigin?: (origin: string) => boolean, delay?: number): void; -} \ No newline at end of file +} From 9540e2de000e6116103f168e9ba7f2a1c545bae1 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Tue, 13 May 2014 00:05:07 +0800 Subject: [PATCH 3/6] Use standard header template in definition file. --- jquery.postMessage/jquery.postMessage.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.postMessage/jquery.postMessage.d.ts b/jquery.postMessage/jquery.postMessage.d.ts index 2ac58ce91..949454038 100644 --- a/jquery.postMessage/jquery.postMessage.d.ts +++ b/jquery.postMessage/jquery.postMessage.d.ts @@ -1,8 +1,8 @@ // Type definitions for jQuery postMessage // Project: http://benalman.com/projects/jquery-postmessage-plugin/ // Definitions by: Junle Li +// Definitions: https://github.com/borisyankov/DefinitelyTyped // Official docs: http://benalman.com/code/projects/jquery-postmessage/docs/files/jquery-ba-postmessage-js.html -// License: MIT license http://mit-license.org/ interface JQueryStatic { From 56f52b48eccdd8685f2aa4baf968cd9ff26603e4 Mon Sep 17 00:00:00 2001 From: Junle Li Date: Tue, 13 May 2014 00:18:29 +0800 Subject: [PATCH 4/6] Add some small tests for jQuery postMessage definition file. --- jquery.postMessage/jquery.postMessage-test.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 jquery.postMessage/jquery.postMessage-test.ts diff --git a/jquery.postMessage/jquery.postMessage-test.ts b/jquery.postMessage/jquery.postMessage-test.ts new file mode 100644 index 000000000..3336455cd --- /dev/null +++ b/jquery.postMessage/jquery.postMessage-test.ts @@ -0,0 +1,28 @@ +/// +/// + +function test_postMessage() { + // post plain message + $.postMessage('test message', 'http://dummy.url/', parent); + + // post object message + $.postMessage({ + 'a': '1', + 'b': '2' + }, 'http://dummy.url/', parent); +}; + +function test_receiveMessage() { + // receive plain source origin + $.receiveMessage((e) => { + // e is an instance of MessageEvent + console.log(e.data); + console.log(e.source); + console.log(e.origin); + }, 'http://dummy.url'); + + // receive source origin callback + $.receiveMessage((e) => {}, (sourceOrigin) => { + return sourceOrigin === 'http://dummy.url'; + }, 100); +}; \ No newline at end of file From 276f88c4d54729e2627c33964dc5c81475e3052f Mon Sep 17 00:00:00 2001 From: Junle Li Date: Tue, 13 May 2014 00:28:05 +0800 Subject: [PATCH 5/6] Add new line at the end of the test file. --- jquery.postMessage/jquery.postMessage-test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jquery.postMessage/jquery.postMessage-test.ts b/jquery.postMessage/jquery.postMessage-test.ts index 3336455cd..077063303 100644 --- a/jquery.postMessage/jquery.postMessage-test.ts +++ b/jquery.postMessage/jquery.postMessage-test.ts @@ -25,4 +25,4 @@ function test_receiveMessage() { $.receiveMessage((e) => {}, (sourceOrigin) => { return sourceOrigin === 'http://dummy.url'; }, 100); -}; \ No newline at end of file +}; From 92466814d5ad9e6df75e3e7cfdbda6e8eb2d2577 Mon Sep 17 00:00:00 2001 From: vvakame Date: Tue, 20 May 2014 16:20:36 +0900 Subject: [PATCH 6/6] rename jquery.postMessage/jquery.postMessage-test.ts -> jquery.postMessage/jquery.postMessage-tests.ts --- .../{jquery.postMessage-test.ts => jquery.postMessage-tests.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename jquery.postMessage/{jquery.postMessage-test.ts => jquery.postMessage-tests.ts} (100%) diff --git a/jquery.postMessage/jquery.postMessage-test.ts b/jquery.postMessage/jquery.postMessage-tests.ts similarity index 100% rename from jquery.postMessage/jquery.postMessage-test.ts rename to jquery.postMessage/jquery.postMessage-tests.ts