Advertisement
Guest User

Untitled

a guest
Apr 20th, 2014
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. var port = null;
  2. function setPort() {
  3. chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
  4. port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
  5. });
  6. }
  7.  
  8. // when i click on something, get the port and send a message
  9. function clickHandler(e) {
  10. setPort();
  11. if (port) {
  12. port.postMessage({key: 'message', value: true});
  13. }
  14. }
  15.  
  16. chrome.runtime.onConnect.addListener(function (port) {
  17. if (port.name == "CONTENTSCRIPT") {
  18. port.onMessage.addListener(function (msg) {
  19. console.log(msg);
  20. });
  21. }
  22. });
  23.  
  24. function setPort(callback) {
  25. chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
  26. port = chrome.tabs.connect(tabs[0].id, {name: "CONTENTSCRIPT"});
  27. callback(port);
  28. });
  29. }
  30.  
  31. function clickHandler(e) {
  32. setPort( function (port) {
  33. if (port) { port.postMessage({key: 'message', value: true}); }
  34. });
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement