Tuesday, January 5, 2010

Google Chrome: content script limitations make my extension look bad

After I published my Google Chrome extension Boss Key and Button, some positive feedbacks were received in the beginning. But later on, I kept receiving comments saying that the the extension was not working. That puzzled me for quite some time because I had developed it in a quite early version of Google Chrome (the dev channel) and tested it on both Windows and Linux successfully.

Then I realized that it might be the limitations of the content scripts that confuse the user. Because the background page could not capture the key strokes or the mouse clicks, I had to use a content script to add event listeners to the key and mouse events. What pages a content script could run in was defined in the manifest.json file with a matching pattern. Unfortunately, the matches parameter did not have a pattern of everything. I.e. You could not add a pattern of *://*/*. So a content script could only works on pages of http://, https://, ftp:// and file://. When a user opened a new tab in Chrome, the new tab had a default URI of chrome://newtab/. A content script would not run in it. In another word, my extension could not work when a user pressed the Boss Key and Button on that page.

The second reason could be that a user tried to use the extension when he was browsing the Google Chrome extensions gallery. For some reasons I don't know, content scripts listed in manifest.json would not run in the pages on that site. Users tended to try a extension as soon as it was installed so it was highly possible that they installed my Boss Key and Button extension, pressed the key or button and nothing happened because they were still on the Google Chrome extensions site. Then they got frustrated and removed the extension right away and missed the chance to try it on other pages. This might be the reason why I received positive feedbacks when I published version 1.0 on another site and got negative ones when the new version was published on Google Chrome extensions gallery.

The third reason could be that a content script only ran when the page was being loaded. Supposed a user had opened several pages, when he installed a new extension, the content scripts would not run in those pages. He needed to reload those pages to make the content script work on them, or just restart the Chrome browser. But an innocent user would not know that, he would assume the extension was bad instead and uninstall it immediately.

I have not found a way to solve the problem caused by the first reason. For the second and third reasons, we could use a trick of explicitly executing the content script by calling function chrome.tabs.executeScript(). That was what I did in the new version of Boss Key and Button extension. However, sometimes you may not want the content script being executed multiple times on a page, then you need some kind of flag to mark the executing of the content script.

I hope this blog can help a little if you are using content scripts in your extension.

1 comment:

DrKeithCurrie said...

Google is a faster way of web search.everyone can access it's free.

Get This <