JavaScript編寫Chrome擴(kuò)展實(shí)現(xiàn)與瀏覽器的交互及時間通知
和瀏覽器的交互
1、書簽
使用chrome.bookmarks模塊來創(chuàng)建、組織和管理書簽。也可參看 Override Pages,來創(chuàng)建一個可定制的書簽管理器頁面。
1.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [
"bookmarks"
],
...
}
對象和屬性:
簽是按照樹狀結(jié)構(gòu)組織的,每個節(jié)點(diǎn)都是一個書簽或者一組節(jié)點(diǎn)(每個書簽夾可包含多個節(jié)點(diǎn))。每個節(jié)點(diǎn)都對應(yīng)一個BookmarkTreeNode 對象。
可以通過 chrome.bookmarks API來使用BookmarkTreeNode的屬性。
例子:
創(chuàng)建了一個標(biāo)題為 "Extension bookmarks"的書簽夾。
chrome.bookmarks.create({'parentId': bookmarkBar.id,
'title': 'Extension bookmarks'},
function(newFolder) {
console.log("added folder: " + newFolder.title);
});
創(chuàng)建了一個指向擴(kuò)展開發(fā)文檔的書簽。
chrome.bookmarks.create({'parentId': extensionsFolderId,
'title': 'Extensions doc',
'url': 'http://code.google.com/chrome/extensions'});
2、Cookies
2.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [
"cookies",
"*://*.google.com"
],
...
}
3、開發(fā)者工具
下列API模塊提供了開發(fā)人員工具的部分接口,以支持您對開發(fā)人員工具進(jìn)行擴(kuò)展。
(1)devtools.inspectedWindow
(2)devtools.network
(3)devtools.panels
3.1、manifest.json 中配置
{
"name": ...
"version": "1.0",
"minimum_chrome_version": "10.0",
"devtools_page": "devtools.html",
...
}
4、Events
Event 是一個對象,當(dāng)你關(guān)注的一些事情發(fā)生時通知你。 以下是一個使用 chrome.tabs.onCreated event 的例子,每當(dāng)一個新標(biāo)簽創(chuàng)建時,event對象會得到通知:
chrome.tabs.onCreated.addListener(function(tab) {
appendToLog('tabs.onCreated --'
+ ' window: ' + tab.windowId
+ ' tab: ' + tab.id
+ ' index: ' + tab.index
+ ' url: ' + tab.url);
});
你可以調(diào)用任何 Event 對象的以下方法:
void addListener(function callback(...)) void removeListener(function callback(...)) bool hasListener(function callback(...))
5、瀏覽歷史
chorme.history 模塊被用于和瀏覽器所訪問的頁面記錄交互。你可以添加、刪除、查詢?yōu)g覽器的歷史記錄。
5.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [
"history"
],
...
}
6、插件管理
chrome.management 模塊提供了管理已安裝和正在運(yùn)行中的擴(kuò)展或應(yīng)用的方法。對于重寫內(nèi)建的新標(biāo)簽頁的擴(kuò)展尤其有用。
要使用這個API,您必須在擴(kuò)展清單文件中 中對授權(quán)。
6.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [ "management" ],
...
}
7、標(biāo)簽
chrome標(biāo)簽?zāi)K被用于和瀏覽器的標(biāo)簽系統(tǒng)交互。此模塊被用于創(chuàng)建,修改,重新排列瀏覽器中的標(biāo)簽。
7.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": [
"tabs"
],
...
}
8、視窗
使用chrome.windows模塊與瀏覽器視窗進(jìn)行交互。 你可以使用這個模塊在瀏覽器中創(chuàng)建、修改和重新排列視窗。
8.1、manifest.json 中配置
{
"name": "My extension",
...
"permissions": ["tabs"],
...
}
時間通知(notifications)的實(shí)現(xiàn)
1、創(chuàng)建notification的兩種方法:
// 注意:沒有必要調(diào)用 webkitNotifications.checkPermission()。 // 聲明了 notifications 權(quán)限的擴(kuò)展程序總是允許創(chuàng)建通知。 // 創(chuàng)建一個簡單的文本通知: var notification = webkitNotifications.createNotification( '48.png', // 圖標(biāo) URL,可以是相對路徑 '您好!', // 通知標(biāo)題 '內(nèi)容(Lorem ipsum...)' // 通知正文文本 ); // 或者創(chuàng)建 HTML 通知: var notification = webkitNotifications.createHTMLNotification( 'notification.html' // HTML 的 URL,可以是相對路徑 ); // 然后顯示通知。 notification.show();
2、通知與其他頁面的通信方式:
// 在一個通知中...
chrome.extension.getBackgroundPage().doThing();
// 來自后臺網(wǎng)頁...
chrome.extension.getViews({type:"notification"}).forEach(function(win) {
win.doOtherThing();
});
3、時間通知的實(shí)例
下面就創(chuàng)建一個時間通知,每個10秒鐘彈出一次時間提醒,一共彈出10次。

3.1、manifest.json
{
// 這個字段將用在安裝對話框,擴(kuò)展管理界面,和store里面,彈出通知的標(biāo)題
"name": "系統(tǒng)通知",
// 擴(kuò)展的版本用一個到4個數(shù)字來表示,中間用點(diǎn)隔開,必須在0到65535之間,非零數(shù)字不能0開頭
"version": "1",
// 描述擴(kuò)種的一段字符串(不能是html或者其他格式,不能超過132個字符)。這個描述必須對瀏覽器擴(kuò)展的管理界面和Chrome Web Store都合適。
"description": "Shows off desktop notifications, which are \"toast\" windows that pop up on the desktop.",
// 一個或者多個圖標(biāo)來表示擴(kuò)展,app,和皮膚
"icons": {
"16": "16.png", // 應(yīng)用的fa網(wǎng)頁圖標(biāo)
"48": "48.png", // 應(yīng)用管理頁面需要這個圖標(biāo)
"128": "128.png" // 在webstore安裝的時候使用
},
// 擴(kuò)展或app將使用的一組權(quán)限
"permissions": ["tabs", "notifications"],
// Manifest V2 用background屬性取代了background_page
// 這里指定了一個Javascript腳本
"background": { "scripts": ["background.js"] },
// Manifest version 1在Chrome18中已經(jīng)被棄用了,這里應(yīng)該指定為2
"manifest_version": 2,
// manifest_version 2中,指定擴(kuò)展程序包內(nèi)可以在網(wǎng)頁中使用的資源路徑(相對于擴(kuò)展程序包的根目錄)需要使用該屬性把資源列入白名單,插入的content script本身不需要加入白名單
"web_accessible_resources": [
"48.png"
]
}
3.2、background.js
/**
* 顯示一個時間 notification
*/
function show() {
var time = new Date().format('yyyy-MM-dd hh:mm:ss');
// 創(chuàng)建一個notification
var notification = window.webkitNotifications.createNotification(
'48.png', // 圖片,在web_accessible_resources 中添加了
'現(xiàn)在的時間是:', // title
time // body.
);
// 顯示notification
notification.show();
}
// 格式化時間函數(shù)
Date.prototype.format = function(format){
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1 ? o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
// 測試瀏覽器是否支持 webkitNotifications
if(window.webkitNotifications) {
// 顯示通知
show();
var interval = 0;
// 彈出10次
var times = 10;
// 創(chuàng)建定時器
var timer = setInterval(function() {
interval++;
// 10秒鐘彈出一次
if (10 <= interval) {
show();
interval = 0;
times--;
if(times <- 0) clearInterval(timer);
}
}, 1000);
}
源代碼
https://github.com/arthinking/google-plugins/tree/master/example/notifications
相關(guān)文章
layui 實(shí)現(xiàn)二級彈窗彈出之后 關(guān)閉一級彈窗的方法
今天小編就為大家分享一篇layui 實(shí)現(xiàn)二級彈窗彈出之后 關(guān)閉一級彈窗的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
js實(shí)現(xiàn)雙擊單元格變成文本輸入框效果代碼
單擊單元格,即可將其變?yōu)槲谋究?,方便編輯測試2008-04-04
JavaScript實(shí)現(xiàn)簡單的二級導(dǎo)航菜單實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)簡單的二級導(dǎo)航菜單,設(shè)計javascript動態(tài)操作頁面元素的技巧,非常具有實(shí)用價值,需要的朋友可以參考下2015-04-04
基于d3.js/neovis.js/neod3.js實(shí)現(xiàn)鏈接neo4j圖形數(shù)據(jù)庫的圖像化顯示功能
neovis.js?由vis.js支持的圖形可視化以及來自Neo4j的數(shù)據(jù)。這篇文章主要介紹了基于d3.js/neovis.js/neod3.js實(shí)現(xiàn)鏈接neo4j圖形數(shù)據(jù)庫的圖像化顯示功能,需要的朋友可以參考下2022-02-02
asp.net+js 實(shí)現(xiàn)無刷新上傳解析csv文件的代碼
無刷新上傳解析csv文件的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-05-05
微信小程序 下拉列表的實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了微信小程序 下拉列表的實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
JavaScript實(shí)現(xiàn)左右下拉框動態(tài)增刪示例
本篇文章主要介紹了JavaScript實(shí)現(xiàn)左右下拉框動態(tài)增刪示例,可以對下拉框進(jìn)行刪除和增加,非常具有實(shí)用價值,需要的朋友可以參考下。2017-03-03

