Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var port = null;
- function setPort() {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
- });
- }
- // when i click on something, get the port and send a message
- function clickHandler(e) {
- setPort();
- if (port) {
- port.postMessage({key: 'message', value: true});
- }
- }
- chrome.runtime.onConnect.addListener(function (port) {
- if (port.name == "CONTENTSCRIPT") {
- port.onMessage.addListener(function (msg) {
- console.log(msg);
- });
- }
- });
- function setPort(callback) {
- chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
- port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
- callback(port);
- });
- }
- function clickHandler(e) {
- setPort( function (port) {
- if (port) { port.postMessage({key: 'message', value: true}); }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement