javascript 有没有办法将 chrome 扩展程序移植到其他浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3459254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to port a chrome extension to other browsers?
提问by David
Is there a way to port a chrome extension to other browsers, without having to entirely re-write the code? My chrome extension uses the browser_actioncommand to open "popup.html" in the extension window...
有没有办法将 chrome 扩展程序移植到其他浏览器,而不必完全重新编写代码?我的 chrome 扩展使用browser_action命令在扩展窗口中打开“popup.html”...
Update:I found adblockforchrome port.jsand found it only somewhat helpful for porting to Safari...
更新:我找到了adblockforchrome port.js并发现它对移植到 Safari 有点帮助......
回答by David
Apple provides some guidanceon how to port convert a Chrome extension to Safari, but the problem you're going to run into is that each browser has a different set of allowed functionality for extensions. Even with Chrome to Safari, there are things you could do in Chrome you can't do in Safari, and visa versa.
Apple提供了一些关于如何将 Chrome 扩展程序移植到 Safari 的指南,但您将遇到的问题是每个浏览器都有一组不同的扩展程序允许功能。即使使用 Chrome 到 Safari,有些事情您可以在 Chrome 中完成,而在 Safari 中却无法完成,反之亦然。
回答by Stefano
Jumping in a bit late.
入坑有点晚。
Our company - Slice Factory (full disclosure here!) does provide a browser extension conversion service: http://gallery.extensionfactory.com/labs/conversion/In most cases your chrome extension will work seamlessly in FF and Safari.
我们公司 - Slice Factory(在此完全披露!)确实提供浏览器扩展转换服务:http: //gallery.extensionfactory.com/labs/conversion/在大多数情况下,您的 chrome 扩展将在 FF 和 Safari 中无缝运行。
The service is still in beta trial, and Firefox conversion is working better than Safari one. I can't fully disclose what's our technical solution, but this being an SO answer, I can add a few details: we have re-developed a full javascript api stack that mimics most of chrome extension APIs for Safari and Firefox; in Firefox we base our work on JetChrome. Plus we have wrappers that re-package the chrome extension adding our library and rewriting manifests and so on.
该服务仍处于测试阶段,Firefox 转换比 Safari 更好。我不能完全透露我们的技术解决方案是什么,但这是一个 SO 答案,我可以添加一些细节:我们重新开发了一个完整的 javascript api 堆栈,它模仿了 Safari 和 Firefox 的大多数 chrome 扩展 API;在 Firefox 中,我们的工作基于 JetChrome。此外,我们还有包装器可以重新打包 chrome 扩展,添加我们的库和重写清单等等。
Beyond that, it's mostly a few good ideas, trial and error, and a LOT of development time.
除此之外,主要是一些好的想法、反复试验和大量的开发时间。
As an example: Safari 5.1 just introduced a popup corresponding to Chrome's browser_action, but we have already an alternative solution that works for previous versions. Firefox does not support HTML5 WebSQL, but we have an API for it. We also provide a way to bring webapps to FF, and soon to Safari. The objective is to have Chrome API as a reference, and mimic it on all the other browsers.
例如:Safari 5.1 刚刚引入了一个对应于 Chrome 的 browser_action 的弹出窗口,但我们已经有一个适用于以前版本的替代解决方案。Firefox 不支持 HTML5 WebSQL,但我们有一个 API。我们还提供了一种将 webapps 引入 FF 并很快引入 Safari 的方法。目标是将 Chrome API 作为参考,并在所有其他浏览器上模仿它。
Without pushing you towards our solution, I might add that the time to fully cover the Chrome API on both Safari and Firefox is probably not worth it - unless you plan to convert several extensions. So for just one or two extensions I would advise making your code as modular as possible, and just creating three extensions - or trying our service!
在不推动您采用我们的解决方案的情况下,我可能会补充说,在 Safari 和 Firefox 上完全覆盖 Chrome API 的时间可能不值得 - 除非您计划转换多个扩展。因此,对于一两个扩展,我建议使您的代码尽可能模块化,并且只创建三个扩展——或者尝试我们的服务!
回答by Daniel O'Hara
It depends on the type of the extension, but not really. To answer your question more clearly, you need to specify what your extension is about: modify some pages (like userscripts/userstyles), extend browsers' features, or something else.
这取决于扩展的类型,但并非如此。为了更清楚地回答您的问题,您需要指定您的扩展程序的内容:修改某些页面(如用户脚本/用户样式)、扩展浏览器的功能或其他内容。
There are a few portability notes, however, that could help you to simplify this process:
但是,有一些可移植性说明可以帮助您简化此过程:
- Don't write browser specific code. In some modern browsers you're able to use HTML5 features, like Web SQL Database API. It is difficult to emulate such behaviour on IE, for instance.
- Keep your JavaScript modular; don't use vendor specific JavaScript methods, and your code will be portable and will not rely on TraceMonkey or V8;
- Separate HTML from CSS and Javascript, don't make your code dirty and complicated.
- 不要编写浏览器特定的代码。在某些现代浏览器中,您可以使用 HTML5 功能,例如 Web SQL 数据库 API。例如,很难在 IE 上模拟这种行为。
- 保持你的 JavaScript 模块化;不要使用供应商特定的 JavaScript 方法,你的代码将是可移植的,不会依赖于 TraceMonkey 或 V8;
- 将 HTML 与 CSS 和 Javascript 分开,不要让您的代码变得肮脏和复杂。
回答by abhijithvijayan
Yes, it's possible with Webextension API using webextension-polyfill.
是的,使用webextension-polyfill 的 Webextension API 是可能的。
I made use of it to build thiswhere I only had to change the callback methods to Promises and using browser.somethinginstead of chrome.somethingto call the browser APIs.
我使用它来构建这个,我只需要将回调方法更改为 Promises 并使用browser.something而不是chrome.something调用浏览器 API。

