Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. //js/get_var.js
  2. setTimeout(function() {
  3. var info = false;
  4. if (window.mySummaryPrefix) {
  5. info = {mySummaryPrefix:mySummaryPrefix}
  6. }
  7. document.dispatchEvent(new CustomEvent('my_event_connectExtension', {
  8. detail: info
  9. }));
  10. }, 0);
  11.  
  12. ...
  13. "web_accessible_resources": ["js/get_var.js"]
  14. ...
  15.  
  16. //content_script.js
  17. function askForValue(){
  18. var scriptTag = document.createElement('script');
  19. scriptTag.src = chrome.extension.getURL('js/get_var.js');
  20. (document.head||document.documentElement).appendChild(scriptTag);
  21. scriptTag.onload = function() {
  22. scriptTag.parentNode.removeChild(scriptTag);
  23. };
  24. }
  25.  
  26. document.addEventListener('my_event_connectExtension', function(e) {
  27. if (e.detail) {
  28. var info = e.detail && {'info': e.detail} || false;
  29. console.log(info);
  30. }
  31. });
  32.  
  33. document.addEventListener('DOMContentLoaded', function() {
  34. askForValue();
  35. }
  36.  
  37. "content_scripts": [{
  38. "matches": ["https://*/*"], /* your url matchs */
  39. "js": ["js/content_script.js"]
  40. }],
  41.  
  42. //from popup.js
  43. ...
  44. chrome.tabs.getSelected(null, function(tab) {
  45. chrome.tabs.sendMessage(tab.id, {
  46. 'action': 'myExtension:myAction'
  47. }, function(result){
  48. var info = result && result.info;
  49. });
  50. });
  51. ...
  52.  
  53. chrome.runtime.onMessage.addListener(function(request, sender, callback){
  54. if (request.action === 'myExtension:myAction') { ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement