Experimental APIs in Chrome Extensions - #1

This post covers the new Chrome Extension API methods which are in development. Some of which are already in the dev release of Google Chrome (5.0.356.2). Most of these methods will most likely change before they are released in the stable channel, but it will give you an idea of what you can expect from the extension API in the future.

Enabling experimental APIs

To use the experimental methods you need to first turn them on in both Chrome and on per extension basis. Begin with adding --enable-experimental-extension-apis to the command arguments of Chrome. Next find the extension you want to try the experimental APIs on and add experimental to the permission section of the manifest file.

Clipboard (chrome.experimental.clipboard)

Copying text is already possible in Chrome by selecting text in a textarea and executing the execCommand("Copy") method. Though, this is quite a hacky way so it is great to know that this will now be offered through an API method. Methods for cutting and pasting data is also available.

Following is an example of usage:

// Select
document.getElementById('textarea1').select();

// Copy/Cut [executeCopy(tabid, callback)]
chrome.experimental.clipboard.executeCopy(1, function() {
    console.log('copied!');

    // Focus an element
    document.getElementById('textarea2').focus();

    // Paste [executePaste(tabid, callback)]
    chrome.experimental.clipboard.executePaste(1, function() {
        console.log('pasted!');
    });
});

What I am missing is a method where I can pass a string as an argument, which will be copied directly, without needing to select the text beforehand.

Infobar (chrome.experimental.infobars)

chrome-experimental-infobar2You are most likely familiar with infobars from when you login to a web page and Chrome asks if it should remember your login. Infobars can be used when you want to alert users of a completed action, or when you want to give user a choice to take an action (e.g. backup the extensions data).

Following is an example usage:

// Show an infobar [show(details, callback)]
chrome.experimental.infobars.show({ path: 'infobar.html' }, function(window) {
    console.log('Infobar showed, in window ', window);
});

It would be great if we got some default CSS styles for buttons and links for a consistent look, as currently we have to style them ourselves. I am pretty sure this is in the pipeline, though.

Idle (chrome.experimental.idle)

This API is quite interesting. It allows us to query which state (idle, active) Chrome currently is in. This can be useful when you want to run a certain process when Chrome gets in idle state, e.g. pause the music of a music player after being idle for 30 minutes.

Following is an example usage:

// Find out which state Chrome is in. [queryState(threshold, callback)]
chrome.experimental.idle.queryState(15, function(state) {
    console.log('Chrome is in ', state, ' state.');
});

// Event which fires when state changes
chrome.experimental.idle.onStateChanged.addListener(function(state) {
    console.log('Changed to ', state, ' state.');
});

Currently the documentation for this API is not ready (or the API itself for that matter) which means I don’t know which unit the threshold is in, but we can assume it is in seconds.

Stay up to date

As these experimental APIs changes a lot before they are released it is a good idea to check back at the documentation at Google Code regularly. You can also have a look at the latest trunk, more specifically the extension_api.json file, for the absolute latest APIs.

These were only some of the experimental APIs the extension API offers, for more a full documentation see the links above. I will also continue writing these type of posts for the experimental APIs in Chrome.

published in Chrome Extensions