Chrome插件开发学习笔记(二)

本文介绍了Chrome插件开发中涉及的API,包括bookmarks、browser Actions、extensions、history、i18n、page Actions、tabs和windows等模块。详细讲解了各模块的功能和常用方法,如bookmarks的管理、browser Actions的图标、提示和弹出窗口设置,以及extensions的通信和历史记录的交互等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Chrome Supported API

Here are the supported chrome APIs:

-       bookmarks

-       browser Action

-       extension

-       history

-       i18n

-       page Action

-       tabs

-       windows

 

Bookmarks

You must declare the bookmarks permission in the manifest file.

Bookmarks are organized in a tree, where each node in the tree is a either a bookmark or a group (a folder that can contain nodes). Each node in the tree is represented by a BookmarkTreeNode object, whose properties are used throughout the chrome.bookmarks API.

bookmark object

Ø  chrome.bookmarks.get(string or array of string idOrIdList, function callback)

Ø  chrome.bookmarks.getChildren(string id, function callback)

Ø  chrome.bookmarks.getRecent(integer numberOfItems, function callback)

Ø  hrome.bookmarks.getTree(function callback)

Ø  chrome.bookmarks.move(string id, object destination, function callback)

Ø  chrome.bookmarks.remove(string id, function callback)

Ø  chrome.bookmarks.removeTree(string id, function callback)

Ø  chrome.bookmarks.search(string query, function callback)

Ø  chrome.bookmarks.update(string id, object changes, function callback)

 

Browser Actions

A browser action can also have a tooltip, a badge, and a popup. (It must have an icon)

Icon: can be up to 19 pixels wide and high.

Tooltip: use the default_title field of brwser_action in the manifest, or call the setTitle() method. You can specify locale-specific strings for the default_title field.

Badge: a bit of text that is layered over the icon. Badges makes it easy to update the browser action to display a small amount of information about the state of the extension.

Popup: If a browser action has a popup, the popup appears when the user clicks the icon. The popup can contain any HTML contents, and it is automatically sized to fit its contents.

 

Ø  chrome.browserAction.setBadgeBackgroundColor(, object details)

Ø  chrome.browserAction.setBadgeText(, object details)

Ø  chrome.browserAction.setIcon(, object details)

Ø  chrome.browserAction.setPopup(, object details)

Ø  chrome.browserAction.setTitle(, object details)

 

Note: It only support onClicked event.

 

Extensions

This module has utilities that can be used by any extension page. It includes support for exchanging messages between an extension and its content scripts or between extensions.

Ø  chrome.extension.lastError

Ø  chrome.extension.inIncognitoTab

Ø  Port chrome.extension.connect(, string extensionId, object connectInfo)

Ø  DOMWindow chrome.extension.getBackgroundPage(, )

Ø  string chrome.extension.getURL(, string path)

Ø  array of DOMWindow chrome.extension.getViews(, object fetchProperties)

Ø  chrome.extension.sendRequest(, string extensionId, any request, function responseCallback)

 

Note: MessageSender is an object containing information about the script context that sent a message or request.

 

History

Use the history module to interact with the browser’s record of visited pages. You can add, remove and query for URLs in the browser’s history.

 

The history API uses a transition type to describe how the browser navigated to a particular URL on a particular visit. For example, if a user visits a page by clicking a link on another page, the transition type is “link”.

 

Transition type: link, typed, auto_bookmark, auto_subframe, manual_subframe, generated, start_page, form_submit, reload, keyword, keyword_generated.

 

Ø  chrome.history.addUrl(, object details)

Ø  chrome.history.deleteAll(, function callback)

Ø  chrome.history.deleteRange(, object range, function callback)

Ø  chrome.history.deleteUrl(, object details)

Ø  chrome.history.getVisits(, object details, function callback)

Ø  chrome.history.search(, object query, function callback)

 

Note: VisitItem is an object encapsulating one visit to a URL, HistoryItem is an object encapsulating one result of a history query.

 

Internationalization

An internationalized extension can be easily localized – adapted to languages and regions that it didn’t originally support. To internationalize your extension, you need to put all of its user-visible strings into a file named messages.json. Each time you localize your extension you add a messages file under a directory named _locales/localeCode(en, es, ko…)

 

Note: If an extension has a _locales directory, the manifest must define “default_locale”.

The internationalization system provides a few predefined messages to help you localize your extension. (include @@ui_locale or @@bidi_dir…)

 

Here’s how the extension system searches for a message:

1.     Search the messages file (if any) for user’s preferred locale.

2.     If the user’s preferred locale has a region (that is, the local has an underscore), search the locale without that region.

3.     Search the messages file for the extension’s default locale.

 

Ø  chrome.i18n.getAcceptLanguages(, function callback)

Ø  string chrome.i18n.getMessage(, string messageName, string or array of string substitutions)

 

Page Actions

Use page action to put icons inside the address bar. Page Action represent actions that can be taken on the current page, but that aren’t applicable to all pages. Like:

-       Subscribe to this page’s RSS feed

-       Make a slideshow out of this page’s photos

 

Like browser action, page actions have an icon and also have a tooltip and popup; they can’t have badges, however, in addition, page actions can appear and disappear.

 

Ø  chrome.pageAction.hide(, integer tabId)

Ø  chrome.pageAction.setIcon(, object details)

Ø  chrome.pageAction.setPopup(, object details)

Ø  chrome.pageAction.setTitle(, object details)

Ø  chrome.pageAction.show(, integer tabId)

 

Note: page action only contains onClicked event.

 

Tab

Use the tabs module to interact with the browser’s tab system. You can use this module to create, modify and rearrange tabs in the browser. You must declare the tabs permission in your extension’s manifest to use the tabs API.

 

Ø  chrome.tabs.captureVisibleTab(, integer windowId, object options, function callback)

Ø  Port chrome.tabs.connect(, integer tabId, object connectInfo)

Ø  chrome.tabs.create(, object createProperties, function callback)

Ø  chrome.tabs.detectLanguage(, integer tabId, function callback)

Ø  chrome.tabs.executeScript(, integer tabId, object details, function callback)

Ø  chrome.tabs.get(, integer tabId, function callback)

Ø  chrome.tabs.getAllInWindow(, integer windowId, function callback)

Ø  chrome.tabs.getSelected(, integer windowId, function callback)

Ø  chrome.tabs.insertCSS(, integer tabId, object details, function callback)

Ø  chrome.tabs.move(, integer tabId, object moveProperties, function callback)

Ø  chrome.tabs.remove(, integer tabId, function callback)

Ø  chrome.tabs.sendRequest(, integer tabId, any request, function responseCallback)

Ø  chrome.tabs.update(, integer tabId, object updateProperties, function callback)

 

Note: the event supported include onAttached, onCreated, onDetached, onMoved, onRemoved, onSelectionChanged, onUpdated.

 

Windows

Use the windows module to interact with browser windows. You can use this module to create, modify, and rearrange windows in the browser.

 

To use the windows API, you must declare the “tabs”permission in manifest.json. – The window and tabs modules interact so closely we decided to just share one permission between them.

 

Many functions in the extension system take an optional windowld parameter, which defaults to the current window. The current window is the window that contains the code that is currently executing. It’s important to realize that this can be different from the topmost or focused window.

 

Ø  chrome.windows.create(, object createData, function callback)

Ø  chrome.windows.get(, integer windowId, function callback)

Ø  chrome.windows.getAll(, object getInfo, function callback)

Ø  chrome.windows.getCurrent(, function callback)

Ø  chrome.windows.getLastFocused(, function callback)

Ø  chrome.windows.remove(, integer windowId, function callback)

Ø  chrome.windows.update(, integer windowId, object updateInfo, function callback)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值