国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript編寫Chrome擴(kuò)展實現(xiàn)與瀏覽器的交互及時間通知

2019-11-20 10:02:21
字體:
供稿:網(wǎng)友

和瀏覽器的交互
1、書簽
使用chrome.bookmarks模塊來創(chuàng)建、組織和管理書簽。也可參看 Override Pages,來創(chuàng)建一個可定制的書簽管理器頁面。

1.1、manifest.json 中配置

{ "name": "My extension", ... "permissions": [  "bookmarks" ], ...}

對象和屬性:
簽是按照樹狀結(jié)構(gòu)組織的,每個節(jié)點都是一個書簽或者一組節(jié)點(每個書簽夾可包含多個節(jié)點)。每個節(jié)點都對應(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 模塊提供了管理已安裝和正在運行中的擴(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)的實現(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、時間通知的實例
下面就創(chuàng)建一個時間通知,每個10秒鐘彈出一次時間提醒,一共彈出10次。

2016516161415314.png (315×87)

3.1、manifest.json

{ // 這個字段將用在安裝對話框,擴(kuò)展管理界面,和store里面,彈出通知的標(biāo)題 "name": "系統(tǒng)通知", // 擴(kuò)展的版本用一個到4個數(shù)字來表示,中間用點隔開,必須在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;}// 測試瀏覽器是否支持 webkitNotificationsif(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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 清涧县| 同江市| 汝阳县| 屏东市| 柯坪县| 锡林浩特市| 万载县| 汉中市| 黄骅市| 衡阳市| 隆尧县| 双牌县| 开鲁县| 邹平县| 梁山县| 金昌市| 揭东县| 常宁市| 深圳市| 雷州市| 西藏| 雷波县| 即墨市| 罗定市| 城步| 三穗县| 江陵县| 大方县| 湖北省| 商水县| 仁怀市| 吕梁市| 凤阳县| 洪洞县| 乌拉特前旗| 吉隆县| 岱山县| 张家口市| 绥化市| 长丰县| 崇信县|