admin管理员组文章数量:1277303
Im injecting a script in webpage via content script. Inside the script I am using chrome.runtime.sendMessage to successfully send a message to background script. However I have the extensionId hardcoded. How would I dynamically inject the extension id in webpage to send messages to background script?
chrome.runtime.sendMessage(extensionIdHardCoded, {
msg: data
},
function(response) {});
Im injecting a script in webpage via content script. Inside the script I am using chrome.runtime.sendMessage to successfully send a message to background script. However I have the extensionId hardcoded. How would I dynamically inject the extension id in webpage to send messages to background script?
chrome.runtime.sendMessage(extensionIdHardCoded, {
msg: data
},
function(response) {});
Share
Improve this question
asked Dec 22, 2015 at 2:18
atomatom
1673 silver badges10 bronze badges
2
- 1 Unfortunately, it must be hardcoded. – Daniel Herr Commented Dec 22, 2015 at 3:00
- @DanielHerr are there any security measures I should in this scenario? My extension just manipulated JS on the front end, and has callbacks between the webpage and background script. – atom Commented Dec 22, 2015 at 4:26
2 Answers
Reset to default 13First off, if you already have a content script, you don't have to use externally_connectable
to municate - you could use custom events to municate with the content script that would forward it to background.
That said, you can use chrome.runtime.id
and pass it to the window context before injecting your script:
var script = document.createElement('script');
script.textContent = "var extensionId = " + JSON.stringify(chrome.runtime.id);
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);
/* now inject your script */
Alternatively, you could add an invisible DOM node that would contain the ID as content or some attribute and read that from the injected script.
Use chrome.runtime.id
like this:
chrome.runtime.sendMessage(chrome.runtime.id, {
msg: data
},
function(response) {});
本文标签: javascriptHow to dynamically send chrome extension ID to a webpage for message passingStack Overflow
版权声明:本文标题:javascript - How to dynamically send chrome extension ID to a webpage for message passing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741215776a2360016.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论