admin管理员组文章数量:1415467
I want to call a function in the chrome.tabs.executeScript
that returns a string that I will be sending it in chrome.runtime.sendMessage()
.
code:
chrome.tabs.executeScript(tab.id, {code:var string1= ??? ;
chrome.runtime.sendMessage(string1);"});
How do I do this . My function contains Javascript code that returns a string or an array of strings.
Some help would be appreciated.
Update:
function(array_of_Tabs) {
var tab = array_of_Tabs[0];
//tab.url; - url of the active tab
chrome.tabs.executeScript(tab.id, {code: 'var test = document.body,text= test.textContent,bodyStart="<BODY",bodyEnd="</BODY>",regex=bodyStart+"(.*?)"+ bodyEnd,regexg = new RegExp(regex, "g"),match = regexg.exec(text);' + 'chrome.runtime.sendMessage(match);'});
});
chrome.runtime.onMessage.addListener(function(request) {
document.getElementsByTagName('html')[0].innerHTML = request;
});
This should've worked according to what you told me, But doesn't . Why.?
And yes BTW I am not parsing pure html on the page it may be anything say to for that matter.
I want to call a function in the chrome.tabs.executeScript
that returns a string that I will be sending it in chrome.runtime.sendMessage()
.
code:
chrome.tabs.executeScript(tab.id, {code:var string1= ??? ;
chrome.runtime.sendMessage(string1);"});
How do I do this . My function contains Javascript code that returns a string or an array of strings.
Some help would be appreciated.
Update:
function(array_of_Tabs) {
var tab = array_of_Tabs[0];
//tab.url; - url of the active tab
chrome.tabs.executeScript(tab.id, {code: 'var test = document.body,text= test.textContent,bodyStart="<BODY",bodyEnd="</BODY>",regex=bodyStart+"(.*?)"+ bodyEnd,regexg = new RegExp(regex, "g"),match = regexg.exec(text);' + 'chrome.runtime.sendMessage(match);'});
});
chrome.runtime.onMessage.addListener(function(request) {
document.getElementsByTagName('html')[0].innerHTML = request;
});
This should've worked according to what you told me, But doesn't . Why.?
And yes BTW I am not parsing pure html on the page it may be anything say to for that matter.
Share Improve this question edited Nov 30, 2013 at 14:52 rahul888 asked Nov 30, 2013 at 10:37 rahul888rahul888 4132 gold badges9 silver badges18 bronze badges 1- See my ment in your other identical question. – gkalpak Commented Nov 30, 2013 at 16:56
1 Answer
Reset to default 3chrome.tabs.executeScript
has a third, optional argument that has access to the result of your injected code. E.g.
// In `background.js`:
...
const tab = ...
chrome.tabs.executeScript(
tab.id,
{code: 'const string1 = "Hello, world!"; return string1;'},
resultArr => processString1(resultArr[0]));
function processString1(str1) {
alert(`String1 = "${str1}"`);
}
If this does not cover your needs and you still want to use Message Passing:
// In `background.js`:
...
const tab = ...
chrome.tabs.executeScript(
tab.id,
{
code: 'const string1 = "Hello, world!"; ' +
'chrome.runtime.sendMessage({text: string1});',
});
chrome.runtime.onMessage.addListener(msg => {
if (msg.text !== undefined) {
alert(`String1 = "${msg.text}"`);
}
});
本文标签: javascriptCalling a function in chrometabsexecuteScriptStack Overflow
版权声明:本文标题:javascript - Calling a function in chrome.tabs.executeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745177580a2646315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论