1

I have to make a firefox addon that searches the loaded page against a list of words (potentially 6500 words) and highlight matches and show synonyms on hover.

So i am using HightlightRegex.js that traverses the dom and searches based on a regex which is using the regex \bMyWord\b.

The main problem is when testing the addon on a page that has many occurrences of the search word, Firefox hangs for a while (5-6 sec) and then highlights are shown. This is happening for 1 word so one can just imagine what will happen if i search 6500 words.

So is there any way that i can run the pageMod in a background thread or asynchronously and highlight words as they are matched without freezing the UI?

You can have a look at the add-on at https://builder.addons.mozilla.org/addon/1042263/latest/

Currently the add-on is not tied to separate tabs and run as a whole on the browser but i doubt that would cause Firefox to hang.

I need to do this as efficiently as possible so suggestion are very welcome.

4

3 回答 3

1

正如 canuckistani 暗示的那样,更好的解决方案只需要两个同步的 DOM 操作:读取和写入。撕掉整个页面(或者更好的是,只撕掉它的<body>)并将其发送到异步工作者或线程,该线程将执行突出显示。完成后,worker 会发出一个事件并传递突出显示的内容,插件现在可以将其插入回页面中。

这样,唯一完成的同步操作是快速、廉价的操作,而其余操作是异步完成的,远离主线程。但是,canuckistani 建议将页面加载到page-worker: 中,因为该页面已经加载到选项卡中,因此无需这样做。只需加载一个假页面并插入实际内容。

于 2012-05-20T05:12:51.750 回答
1

您可以尝试的一件事是使用 page-worker 模块来加载页面并对其进行处理:

https://addons.mozilla.org/en-US/developers/docs/sdk/1.6/packages/addon-kit/page-worker.html

而且,正如 Wladimir 建议的那样,只使用异步代码来搜索文档文本以避免锁定 firefox。

于 2012-04-24T21:51:23.030 回答
1

DOM 通常不是线程安全的,您不能从主线程以外的任何地方访问它。唯一的解决方案是将工作分解成更小的块并使用setTimeout(..., 0)异步运行下一个块,而不阻塞所有内容。

于 2012-04-24T14:14:03.830 回答