Created
November 9, 2015 19:53
-
-
Save kdzwinel/f74910254ce97d7ccbd2 to your computer and use it in GitHub Desktop.
browser-sync-client Chrome Extension socket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
var MSG_NAMESPACE = 'browser-sync'; | |
var port = chrome.runtime.connect({name: MSG_NAMESPACE}); | |
/** | |
* @returns {string} | |
*/ | |
exports.getPath = function () { | |
return window.location.pathname; | |
}; | |
/** | |
* @param name | |
* @param data | |
*/ | |
exports.emit = function (name, data) { | |
//console.log('sendMessage', name, data); | |
data = data || {}; | |
data.url = exports.getPath(); | |
port.postMessage({ | |
action: name, | |
data: data | |
}); | |
}; | |
/** | |
* @param name | |
* @param callback | |
*/ | |
exports.on = function (name, callback) { | |
//console.log('create listener', name); | |
port.onMessage.addListener(function(msg) { | |
if(msg.action === name) { | |
//console.log('onMessage', msg.action, msg.data); | |
callback(msg.data); | |
} | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment