我也寫了輸入法,而且用 Javascript!

聽完 jserv 在 COSCUP 2012 講的新酷音輸入法專案,決定來把這篇欠很久的文章寫完,順便換個標題。

在 Mozilla 工作,加入 Boot to Gecko 專案的第一個工作是處理 Gaia 的輸入法,弄了大約一兩個月,最後做出了可以在瀏覽器獨立運作(不需要從伺服器選字)的自動選字注音輸入法:

Gaia 注音輸入法「JS 注音」瀏覽器示範網頁

詞庫

詞庫要匯進程式勢必要轉成 JSON 格式。原本是從新酷音的詞庫轉出來的,後來發現 LGPL 跟專案不相符(我們用 Apache),找了很久,最後找到 mjhsieh 維護的小麥注音詞庫。至於轉檔,本來一度寫了 phantomJS 的 script 來處理,後來改用自家的 Javascript shell。比較麻煩的就是 jsshell 要用 -U 啟動不然字串不會被當成 UTF-8。

資料庫

要把 3MB 的 JSON 整包放在記憶體也不是不行,只是這樣放手機是跑不動的。另外的後果就是這樣每次網頁載入就要每次重新下載,還不能保證會不會碰到 HTTP cache。

Gecko 提供的 database 解決方案是 IndexedDB。也說不上來好用或是不好用,反正我沒得選(笑)。比起直接用 key 從 JS Object 拿資料,IndexedDB 的 async 流程當然是比較複雜,但也不會說難用(而且 Disk I/O 本來就應該要 off main-thread 才對)。比較複雜的是一些手機輸入法的進階找字功能(例如輸入「ㄊㄅ」就要查到「台北」)就需要用到 IndexedDB 的 range search 等等。

演算法

我沒有實作像是 tree search 之類的搜尋方式,而是傻傻的用窮舉法,把 N 個音可能的斷詞通通切開然後送進資料庫查詞還有問積分。和文字雲一樣,用了很簡單的演算法但是卻很有效。

剩下的部份與接下來

剩下就是一些 code organization 的問題。牽涉到這麼多 function 的程式不太容易整理好,我自己改寫了兩次,後來被北京的同事 port 成拼音輸入法的時候他又改成 prototype 的寫法,看起來又更整齊。Boot to Gecko(產品名 Firefox OS)最初的上市目標是南美洲,所以中文支援的功能就先放著,先去處理系統的部份。

我有很刻意的把注音的 JS 檔保持著可以讓其他環境與架構順利載入與 fall back 的狀況(還偷偷加了 CommonJS 的 define())。DEMO 網頁連 iPhone 都可以用,沒有 IndexedDB 的瀏覽器也會每次載入詞庫 JSON。有一些 fancy 的 idea 是像是可以用 node-gtk 或是其他方法執行,反攻桌面或是 Android 之類的。不過這記憶體消耗比別人大,演算法也沒有比別人強,要做只是證明因為做的到吧 … XD

有什麼意見還請不吝賜教。

Common misconceptions about Boot-to-Gecko, and the Web

中文版刊登於謀智台客部落格。

Being asked to introduce the Boot to Gecko project to various audiences, I think it’s appropriate for me to write some FAQ here on common misconception of the project, and the Web in general.

Misconception #1: Boot to Gecko is yet another mobile platform for apps.

To run you apps on Boot to Gecko (and any other devices with a browser), you would just have to deliver your apps on the plain old-school Web on Internet.

Boot to Gecko is aim to (re)boot to the web. That is, to bring the Open Web to a level that it could compete with proprietary mobile platforms. To application developers, the Web is yet another platform to develop (you would need to port your apps from Obj-C/Java to HTML5), but the good news is your app would now becomes the cross-platform web app, not B2G Apps, PhoneGap Apps, nor Metro-style Windows 8 Apps, and it’s always available to everyone on every platform, one web address away. It would also make your app free from platform vendor control, since no one owns the Web.

Misconception #2: Phone need to be always on-line to use Boot to Gecko and web apps.

Even though every apps on B2G phone is from a web address, even the home screen itself, offline capability can be easily achieved with HTML5 Offline AppCache technology. It’s something that can be done today, available to many browsers available on both desktop and mobile. The only thing you should take care of is how network dependent your app really is. Even on iOS, Twitter app or Facebook app is useless without network connection. You should define a clear boundary between program assets and online information in your HTML5 app. After all, it’s not just an website anymore.

Misconception #3: Web app is crappier than “native apps” in turns of UX.

This is a notion I strongly disagree. The only reason mobile browsers on devices perform worse than the native app is because the venders of the device did not invest enough effort on it. There is a conflict of interest here. Limiting the capability of APIs accessible, besides permission management issue, is the same thing. At Mozilla, we can show that with proper engineering, mobile browser, or web runtime, can run smoothly as silk on devices and deliver the so-called “native” experiences. Other venders investing the web is also doing the same thing. For devices running B2G or Chrome OS, the native app is web apps, there is no point to make it slow, intentionally or unintentionally.

Misconception #4: Web apps running on B2G is dependent on B2G or Mozilla Web APIs.

Yes and no. If you need specific access like SMS message database or phone dialing, initially B2G would be the only platform that make these features available to you. However, we design these Web APIs with standardization in mind, and work closely with standardizing bodies to made these APIs ready for other venders to implement. Evidently, our Web APIs does not live under PhoneGap.*, Windows.*, nor Ti.*; they are design to be at where it should be, like navigator.* (Sure, as experimental features, they came with moz prefix right now, and usual feature detection is advised).

If you choose other packaged web app solution, then, your web app is depend on it. Forever. Mozilla would like you to develop and distribute apps directly to the Web, and you should.

Misconception #5: Apps on Web are free of charge, developers will never make money out of it.

Yeah, like no one had ever become billionaire because of applications or services they put on the web.

There are tons of way to provide services on the web in exchange for money, surely there are ways to make money with web apps, other than bagging for US$0.99 from users. Sure, closed platform with single distribution “app store” seems effective in terms of delivering revenue to developers (besides having 30% of your money being taken along the away), but Mozilla believes choices matters, and an open platform deliver choices to both users and developers.

Mozilla is also exploring effective distribution and monetize channel for the Open Web, in our Open Web App Project and the Mozilla Marketplace website. The goal of offering choices and freedom is deeply embedded in the feature design, both in browser App API and in storefront, and with user authentication and authorization (which is the scope of the Mozilla Persona project, previously known as BrowserID).

Conclusion

Just like Mozilla did with Firefox years ago, we intend to deliver great product that could influence the market, and let the market influence other venders. No one owns the Web, and no one should. Mozilla is here not to make money or become monopoly, but to bring the goodness to the Web that could drive innovation and opportunities centuries to come.

The CTO of Opera Software, Håkon Wium Lie, once said the Web will last for at least 500 years, and made impact to the society just like Gutenberg’s printing press. I totally agree with him, even though none of us will be around and told me I was wrong, as he puts it. Let’s make it great.

Disclaimer: This post express the opinion of my own and does not necessarily represents the point of view of Mozilla nor Mozilla Corp.

To Mozilla HQ: It’s about time to launch a Firefox OS Project

I’ve talked about Firefox OS during several offline occasion; some people find the concept hard to understand, some, agreed with me, suggested that I should put everything together in written so people at large would know about it. Here are the reasons why I believe Mozilla should start working on an operating system based on Firefox.

1. To enrich the add-on ecosystem

Before Apple’s iTunes Apps Store for iPhone, and iPod or iPad later, Mozilla Add-ons was one of the largest collections of web software. The add-ons does from simply help people to access content to development tools. However, after 5 years of introduction of Firefox and it’s extension framework, people find it hard to formulate a business model with it – Mozilla try to cope the problem with donation system incorporated within the add-on site, however, with the proven model from the competitors, developers had shift their focus to these new platforms. Echofon, once called TwitterFox, would not be able to deliver a paid “pro” version successfully if it had not reach other platforms.

By developing an Firefox OS, Mozilla could make proof to developers in the ecosystem that Firefox is more than a browser, and their hard work is more than browser add-ons. I am not saying turning Firefox into an OS would immediately make add-ons development profitable – however, it might not never be if there is no OS.

2. To provide an alternative browser OS

When I talked about Firefox OS, people often compare my imaginary OS with Chrome OS. It’s comparable, yet, the Google’s approach might not be the best way. For instance, Google intend to copy Apple’s business model, creating an centralized Chrome Web Store, defeat the purpose that web should be a platform without a controlling vendor.

Should Mozilla had working on a Firefox OS, it not only provide an alternative to Chrome OS, but it also give users valuable choices on browser OS front. Alternative is not about playing the RMS card – that every software should have a libre alternative – instead, the existence of the strong enough alternative is the key to drive innovation.

(Don’t believe me? Think about IE6.)

3. To deliver better user experience in Firefox

The recent development of Firefox 4 bring exciting UI changes that align with many modern browsers, like Application Tabs or TabCandy, later known as Panorama, which enables user new innovate ways to organize information and works.

These powerful features, nevertheless, doesn’t work within an application as tiny as a browser sometimes. Before Panorama, I usually group my works in different browser windows, so I could switch between them on Windows taskbar. After trying Panorama, along with Application Tab, I don’t think it works as well as Exposé in Mac OS X, or even what I did. For example, app tabs are always visible no matter what Panorama work space you selected, yet in Panorama view app tabs actually belongs to a group – this results confusion. What’s worse, click in any app tab in another work group immediately brings you to that group, with no fast way to switching back.

Another example would be the UI change decision about bringing tabs on top of the toolbar. I agreed with the decision to bring tabs to top; but when I pressed F11 to make my Firefox window go full screen, I found the close window button (the X) was not on the top-right corner of the screen (which, according to Fitt’s law, is a hot spot for cursor aiming, and, according to the logic of Windows OS, should be always the place to close a maximized window).

These examples might be just tiny issues that come with a beta version of Firefox; I believe most of them will be fixed by talent people at Mozilla before the final version releases. Nonetheless, these are the evidence that how messy it would be to ask an browser within an operating system to manage it’s own tasks – We should definitely work on these user experience innovation, however a Firefox browser will always be a constraint for these interaction innovations – A Firefox OS will not. By developing a Firefox OS with these innovations, Mozilla will certainly have a better chance to polish them before they were brought to the browser.

Ever since Windows 95, binding between tasks in browser window and tasks in client applications has always been a key issue. Windows 7 introduced jump list; IE9 users will be able to pin websites on their Windows task bar. A Firefox OS will help Mozilla developers come up with even better interaction, instead of endlessly implement what Windows, or other OS APIs enables applications (browsers) to do.

4. To defend the Open Web

Mozilla is a foundation with limited resource. Everything it does has to be somehow connect to its mission. A Firefox OS project will undeniably connect to the cause. Here is one of the reasons why: Today, users prefer mobile apps than [mobile] web.

I twitted the link sometimes ago. The sad inconvenient truth is, if people access information mainly from apps but not the through the browser, the browser itself will be irrelevant. Eventually, the web itself will be irrelevant, along with the promises of the Open Web that Mozilla holds true. Yet, this is exactly what is happening right now on the mobile front.

(Stilling thinking about bring a full Firefox to iOS is important? IMO it brings more buzz than piratical use.)

Firefox OS has the potential to become not just browser OS for web platform but also a truly open platform. Think about a slightly exaggerated future, where desktops, OSes, and applications on it seize to exist. People would access information on the Internet without going through the Web (and HTTP) but using devices and site-specific applications using socket connections. In that future, there are better be a vendor that continue express interests of openness through technologies and end-user products, and let that vendor be Mozilla, even though the Web as we know it seize to exist. Firefox OS could be that end-user product, just like Firefox 1.0 did to the web almost 6 years ago. Firefox OS could ultimately transform itself beyond browser, or browser OS, but first Mozilla need to make a browser OS.

Thus, to Mozilla HQ: It’s about time to launch a Firefox OS Project.