检查 chrome 扩展程序中访问的每个 url 的最佳实践是什么?
在 Chrome 扩展中,我想根据可能的 url 列表检查每个 url,然后在访问其中一个可能的 url 时执行操作。例如,如果 Amazon.com 位于列表中,则只要用户访问 Amazon.com,就会执行该操作。
大约有 5000 个可能的 url,它们都在 mySQL 数据库中,并且可以输出到 JSON 文件。
每次访问页面时,扩展程序是否应该通过检查 JSON 文件来检查每个 url?无论如何,是否可以将 JSON 文件加载到扩展中?有更好的方法吗?
In a chrome extension I want to to check each url against a list of possible urls and then perform an action if one of the possible urls is visited. For example if Amazon.com was on the list then anytime a user went to Amazon.com the action would be performed.
There are around 5000 possible urls which are all in a mySQL database and can be outputted to a JSON file.
Should the extension check each url everytime a page is visited by checking the JSON file? Is there anyway of loading the JSON file into the extension? Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有(至少)两个选择,具体取决于您在访问特定 URL 时要执行的操作:
您可以在 JavaScript 对象中对过滤器进行编码,该对象在启动时加载到扩展程序的后台页面中。 KMOO 扩展是一个很好的示例,说明了其工作原理:https://code.google.com/p/chrome-opt-out-extension/source/browse/#svn%2Ftrunk%2Fchrome
如果您要注入内容脚本,您可以通过添加适当的过滤器直接在清单中限制您的扩展。请参阅 http://code.google.com 上的
matches
属性例如 /chrome/extensions/content_scripts.html。第一个选项可能是我推荐的。在启动时将大型 JSON 对象解析到查找表中并不是非常昂贵,而且肯定比每次想要做出决定时都读取它要好。
You have (at least) two options, depending on the action you'd like to take when a particular URL is visited:
You can encode your filter in a JavaScript object that you load into the extension's background page at startup. The KMOO extension is a good example of how this might work: https://code.google.com/p/chrome-opt-out-extension/source/browse/#svn%2Ftrunk%2Fchrome
If you're injecting content scripts, you can restrict your extension directly in the manifest by adding appropriate filters. See the
matches
attribute at http://code.google.com/chrome/extensions/content_scripts.html for example.The first option is probably what I'd recommend. Parsing a large JSON object into a lookup table once at startup isn't terribly expensive, and certainly better than reading it every time you'd like to make a decision.