简体   繁体   English

chrome扩展-将消息从内容脚本发送到后台页面,然后再发送回内容脚本?

[英]chrome extension - send message from content script to background page and back to content script?

I have no idea if this is the most efficient way of doing what I'm trying to do, if isn't by all means tell me. 我不知道这是否是我尝试做的最有效的方法,如果不是一定要告诉我的话。

I'm building a chrome extension that is supposed to loop through each word on the page, and then compare it to a list of 12,000 elements in an array. 我正在构建一个chrome扩展程序,该扩展程序应该遍历页面上的每个单词,然后将其与数组中12,000个元素的列表进行比较。 I managed to make it work on the content script page, but to my dismay, it takes about 7 seconds to load my content script because of all the looping that is taking place. 我设法使其在内容脚本页面上工作,但令我沮丧的是,由于发生了所有循环,因此加载内容脚本大约需要7秒钟。 This isn't ideal, obviously. 显然,这并不理想。 So I had the idea of sending all the p tags to the background page, where it then compares the p words to the array, and once it figures out which match, it sends a message back to the content script with a smaller array of just the words that are located both in the p tags and in my array. 因此,我想到了将所有p标记发送到后台页面的想法,然后在其中将p字与数组进行比较,一旦找出匹配项,就会使用较小的数组(仅包含一个)将消息发送回内容脚本位于p标签和我的数组中的单词。

Now my question is, is this possible? 现在我的问题是,这可能吗? Will it reduce the load time? 会减少加载时间吗? Is this efficient? 这样有效吗?

One final question: My manifest is using a background page in the form of html but if I wanted to also use a background script Google doesn't allow that, so can I place my background script in the background page html file and will it load that script? 最后一个问题:我的清单正在使用html形式的背景页面,但是如果我也想使用背景脚本,Google不允许这样做,那么我可以将背景脚本放在背景页面html文件中并将其加载吗?那个剧本?

For your first question, yes it is possible to do this. 对于第一个问题,是的,可以这样做。 It will execute your heavy treatment in an the background (and hidden) page. 它将在后台(和隐藏)页面中执行您的沉重处理。 The page with the content script will not freeze this way. 带有内容脚本的页面不会以这种方式冻结。

You can do it like this : 您可以这样做:

In your background script : 在您的背景脚本中:

function heavyStuff(request, sender, sendResponse)
{

    ...
    doSomeThing(request)
    doOtherThing()
    ...

    sendResponse(youreSmallerArray)
}

chrome.runtime.onMessage.addListener(heavyStuff)

And in your content script : 在您的内容脚本中:

function manageResponse(smallerArray)
{
    ...
    doWhatYouWant(smallerArray)
    ...
}

arrayOfP = $("p")
chrome.runtime.sendMessage(arrayOfP, manageResponse)

So it will do what you want, but are you sure of what you're doing? 这样就可以完成您想要的操作,但是您确定自己在做什么吗? Perhaps your algorithm have to be improved? 也许您的算法需要改进?

For more information about Chrome's message system, you can read the documentation . 有关Chrome的消息系统的更多信息,请阅读文档

Especially if you have to send lot of messages, look for the long-lived connections section. 特别是如果您必须发送大量消息,请查找寿命长的连接部分。


For the second question, you can replace in the manifest the background page attribute by that : 对于第二个问题,您可以在清单中将background page属性替换为:

"background":
{
    "scripts":
    [
        "background.js",
        "otherScript.js",
        ....
        "theLastScript.js"
    ]
}

Chrome will create automaticaly a background page with this scripts included (in the same order). Chrome会自动创建一个包含此脚本的后台页面(顺序相同)。

PS : The next time you post on this site, please ask only one question at a time ;-) PS:下次您在本网站上发帖时,一次只能提出一个问题;-)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将消息从后台脚本发送到内容脚本,然后在Chrome扩展程序中进行响应 - Send message from background script to content script then response in chrome extension 无法从Chrome扩展程序中的后台脚本向内容脚本发送消息 - unable to send message from background script to content script in chrome extension Chrome扩展程序-如何将DOM从内容脚本发送到后台页面? - Chrome Extension - How to message DOM from content script to background page? 从内容脚本发送消息到后台脚本会破坏chrome扩展 - Sending message from content script to background script breaks chrome extension 在 chrome 扩展中,无法从内容脚本向后台脚本发送消息并获得响应 - In a chrome extension, can't send a message from the content script to the background script and get a response Chrome扩展程序背景和内容脚本发布消息 - Chrome extension Background and content script posting message Chrome扩展程序,如何从面板向内容脚本发送消息 - Chrome extension, how to send message from panel to content script 如何将消息从backgroundjs发送到chrome扩展中的内容脚本? - how to send message from backgroundjs to content script in chrome extension? chrome扩展,可将消息从弹出窗口发送到内容脚本 - chrome extension to Send Message from popup to content script Chrome扩展程序背景页面和内容脚本同步 - Chrome Extension Background Page and Content Script Synchronization
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM