chrome extension - alternative to externally_connectable? -


it seems externally_connectable feature allows website communicate extension still in dev channel , not yet stable. there other ways allow specific website communicate extension, while wait feature become stable? how have chrome extension developers traditionally done it?

thanks rob w pointing me in direction of html5 messaging. benefit of other chrome extension developers, i'm writing general problem trying solve , solution worked in end.

i making chrome extension can control music playback on tab via popup player. when user clicks on play/pause/etc on popup player, extension should able convey message webpage , response stating whether action accomplished.

my first approach inject content script music player page. problem is, though, content scripts operate in "sandbox" , cannot access native javascript on page. therefore, content script pretty useless (on own), because while receive commands extension, not effect change on webpage itself.

one thing worked in favor website music playing belongs me, put whatever javascript wanted there , have served server. that's used advantage: created javascript file reside on website , communicate content script mentioned above, via window object of page (i.e. html5 messaging). works because content script , javascript file both exist in same webpage , can share window object of page. rob w pointing me capability. here example of how javascript file on page can initiate connection content script via window object:

content_script.js (injected extension xyz.com):

window.addeventlistener("message", function(event) {     if(event.data.secret_key &&       (event.data.secret_key === "my_secret_key") &&         event.data.source === "page"){         if(event.data.type){             switch(event.data.type) {                 case 'init':                     console.log("received connection request page");                     window.postmessage({source: "content_script", type: 'init',                                          secret_key: "my_secret_key"}, "*");                     break;             }         }     } }, false); 

onpage.js (resides on server , served along xyz.com):

window.postmessage({source: "page", type: 'init',                      secret_key: "my_secret_key"}, "*");  window.addeventlistener("message", function(event) {     if(event.data.secret_key &&        (event.data.secret_key === "my_secret_key") &&         event.data.source === "content_script"){         if(event.data.type){             switch(event.data.type) {                 case 'init':                     console.log("connection established");                     break;             }         }     } }, false); 

i check secret key make sure message originates expect to.

that's it! if unclear, or if have questions, feel free follow up!


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -