While tinkering around with Chrome's Extensions and Javascript, I found some interesting code in some of Chrome's special pages' source.
It turns out that these special pages get an extra function to perform many of their normal activities. This function is: "chrome.send(command_name, [arg1, arg2, ...] )"
Searching through the Chrome source, it seems that Google added a function called "RegisterMessageCallback" to their DOM model, so that special pages could have a few extra commands to achieve the necessary extra functionality found in these special pages. (For example, I doubt that standard Javascript has an API for packaging Extensions, or to cancel downloads...)
A (I believe) full list of these extra functions appears here:
./chrome/browser/extensions/extensions_ui.cc:
dom_ui_->RegisterMessageCallback("requestExtensionsData",
dom_ui->RegisterMessageCallback("toggleDeveloperMode",
dom_ui->RegisterMessageCallback("inspect",
dom_ui->RegisterMessageCallback("reload",
dom_ui->RegisterMessageCallback("enable",
dom_ui->RegisterMessageCallback("uninstall",
dom_ui->RegisterMessageCallback("options",
dom_ui->RegisterMessageCallback("load",
dom_ui->RegisterMessageCallback("pack",
dom_ui->RegisterMessageCallback("autoupdate",
dom_ui->RegisterMessageCallback("selectFilePath",
./chrome/browser/dom_ui/html_dialog_ui.cc:
RegisterMessageCallback("DialogClose",
./chrome/browser/dom_ui/new_tab_ui.cc:
dom_ui_->RegisterMessageCallback("stopPromoLineMessage",
dom_ui->RegisterMessageCallback("getRecentlyClosedTabs",
dom_ui->RegisterMessageCallback("reopenTab",
dom_ui->RegisterMessageCallback("metrics",
dom_ui->RegisterMessageCallback("logEventTime",
dom_ui->RegisterMessageCallback("setHomePage",
./chrome/browser/dom_ui/shown_sections_handler.cc:
dom_ui_->RegisterMessageCallback("getShownSections",
dom_ui_->RegisterMessageCallback("setShownSections",
./chrome/browser/dom_ui/filebrowse_ui.cc:
dom_ui_->RegisterMessageCallback("getRoots",
dom_ui_->RegisterMessageCallback("getChildren",
dom_ui_->RegisterMessageCallback("getMetadata",
dom_ui_->RegisterMessageCallback("openNewPopupWindow",
dom_ui_->RegisterMessageCallback("openNewFullWindow",
./chrome/browser/dom_ui/new_tab_page_sync_handler.cc:
dom_ui_->RegisterMessageCallback("GetSyncMessage",
dom_ui_->RegisterMessageCallback("SyncLinkClicked",
./chrome/browser/dom_ui/tips_handler.cc:
dom_ui_->RegisterMessageCallback("getTips",
./chrome/browser/dom_ui/history_ui.cc:
dom_ui_->RegisterMessageCallback("getHistory",
dom_ui_->RegisterMessageCallback("searchHistory",
dom_ui_->RegisterMessageCallback("deleteDay",
./chrome/browser/dom_ui/downloads_dom_handler.cc:
dom_ui_->RegisterMessageCallback("getDownloads",
dom_ui_->RegisterMessageCallback("openFile",
dom_ui_->RegisterMessageCallback("drag",
dom_ui_->RegisterMessageCallback("saveDangerous",
dom_ui_->RegisterMessageCallback("discardDangerous",
dom_ui_->RegisterMessageCallback("show",
dom_ui_->RegisterMessageCallback("togglepause",
dom_ui_->RegisterMessageCallback("resume",
dom_ui_->RegisterMessageCallback("remove",
dom_ui_->RegisterMessageCallback("cancel",
dom_ui_->RegisterMessageCallback("clearAll",
./chrome/browser/dom_ui/most_visited_handler.cc:
dom_ui_->RegisterMessageCallback("getMostVisited",
dom_ui_->RegisterMessageCallback("blacklistURLFromMostVisited",
dom_ui_->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist",
dom_ui_->RegisterMessageCallback("clearMostVisitedURLsBlacklist",
dom_ui_->RegisterMessageCallback("addPinnedURL",
dom_ui_->RegisterMessageCallback("removePinnedURL",
./chrome/browser/sync/sync_setup_flow.cc:
dom_ui_->RegisterMessageCallback("SubmitAuth",
dom_ui_->RegisterMessageCallback("SubmitMergeAndSync",
For example, looking at downloads_dom_handler.cc, we see that it registered "togglepause" as a function. Let's try it out!
Start a large download. (Something that will take a few minutes to download, so we have time to test this function out.) Now go to the Download Page. Press CTRL+SHIFT+J to launch the Javascript console.
Try running: chrome.send("togglepause", ["0"]); If all went properly (and the id of the download was indeed 0) then the download should be paused. All functions can be invoked in such a way, with the first string being the command name, and the second being a list of arguments to that command, which are all strings. Try looking at the page's source code for examples of how each command is supposed to be called.
Google's official Extension API says that they only support Content-Scripts on http, https, file, and ftp url schemes. However, Chrome's source says that they are considering adding the 'chrome' scheme:
src/chrome/common/extensions/url_pattern.h:
// <url-pattern> := <scheme>://<host><path>
// <scheme> := 'http' | 'https' | 'file' | 'ftp' | 'chrome'
src/chrome/common/extensions/url_pattern.cc:
// TODO(aa): Consider adding chrome-extension? What about more obscure ones
// like data: and javascript: ?
static const char* kValidSchemes[] = {
chrome::kHttpScheme,
chrome::kHttpsScheme,
chrome::kFileScheme,
chrome::kFtpScheme,
};
I sure hope they do. Then, you would be able to use these functions from Extensions by loading that page in a new tab, injecting your own code into the page using a Content Script, and then accessing that code with message-passing. As it is, calling chrome.send() functions is just a nifty trick that has no real use for us.
Unfortunately, I couldn't find the special command I needed. I was hoping to find a way to queue up a download using Javascript, but I didn't see any chrome.send() commands for that. If Google could add that, that would be awesome, since I was working on an extension that would scrape a page for all images/links, and queue every one of them for download. (Similar to DownThemAll! for Firefox.) But without a way to queue a download, my effort is doomed. :(
