Socket
Book a DemoInstallSign in
Socket

cookie-talk

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cookie-talk

A lightweight message-exchange protocol for window to window communication on same domain.

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

A lightweight message-exchange protocol for window to window communication on same domain.

Example

In this example we're going to create 2 html files that are to be open in to browser windows/tabs on the same domain name.

Whatever you type in each window appears in the other window. Messages are sent through channels. Each message is destoryed after being read.

First file:

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Window 1</title>
		<script src="cookie-talk.min.js"></script>
	</head>
	<body>
		<div>
		Write Here:
		<textarea id="input"></textarea>
		</div>
		<hr>
		<div>
		Read here:
		<textarea id="output"></textarea>
		</dive>
	</body>
	<script>
		var ct = cookieTalk.storage;
		var channel1 = new ct('channel1');

		var editor = document.getElementById('input');
		editor.addEventListener('keyup', function(e) {
			channel1.send(editor.value, function() {
				//console.log('sent');
			});
		});

		var channel2 = new ct('channel2');
		var monitor = document.getElementById('output');
		channel2.onMessage(function(message) {
			monitor.value = message;
		})
	</script>
</html>

And second file

<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Window 2</title>
		<script src="cookie-talk.min.js"></script>
	</head>
	<body>
		<div>
		Read here:
		<textarea id="output"></textarea>
		</dive>
		<hr>
		<div>
		Write Here:
		<textarea id="input"></textarea>
		</div>
	</body>
	<script>

		var ct = cookieTalk.factory();
		var channel1 = new ct('channel1');

		var monitor = document.getElementById('output');
		channel1.onMessage(function(message) {
			monitor.value = message;
		})

		var channel2 = new ct('channel2');
		var editor = document.getElementById('input');
		editor.addEventListener('keyup', function(e) {
			channel2.send(editor.value, function() {
			//	console.log('Sent!');
			});
		});
	</script>

</html>

Make sure you have cookie-talk.min.js readable.

Also make sure your document has: <meta charset="utf-8"> in headers otherwise the minified .js does not work.

FAQs

Package last updated on 24 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts