mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 23:08:33 +02:00
Rewrite examples to use browser.* and promises (#138)
This commit is contained in:
@@ -14,18 +14,20 @@ var CATGIFS = "http://chilloutandwatchsomecatgifs.com/";
|
||||
/*
|
||||
Restart alarm for the currently active tab, whenever background.js is run.
|
||||
*/
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
|
||||
gettingActiveTab.then((tabs) => {
|
||||
restartAlarm(tabs[0].id);
|
||||
});
|
||||
|
||||
/*
|
||||
Restart alarm for the currently active tab, whenever the user navigates.
|
||||
*/
|
||||
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
||||
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||||
if (!changeInfo.url) {
|
||||
return;
|
||||
}
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
|
||||
gettingActiveTab.then((tabs) => {
|
||||
if (tabId == tabs[0].id) {
|
||||
restartAlarm(tabId);
|
||||
}
|
||||
@@ -35,7 +37,7 @@ chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
||||
/*
|
||||
Restart alarm for the currently active tab, whenever a new tab becomes active.
|
||||
*/
|
||||
chrome.tabs.onActivated.addListener(function (activeInfo) {
|
||||
browser.tabs.onActivated.addListener((activeInfo) => {
|
||||
restartAlarm(activeInfo.tabId);
|
||||
});
|
||||
|
||||
@@ -44,11 +46,12 @@ restartAlarm: clear all alarms,
|
||||
then set a new alarm for the given tab.
|
||||
*/
|
||||
function restartAlarm(tabId) {
|
||||
chrome.pageAction.hide(tabId);
|
||||
chrome.alarms.clearAll();
|
||||
chrome.tabs.get(tabId, function(tab) {
|
||||
browser.pageAction.hide(tabId);
|
||||
browser.alarms.clearAll();
|
||||
var gettingTab = browser.tabs.get(tabId);
|
||||
gettingTab.then((tab) => {
|
||||
if (tab.url != CATGIFS) {
|
||||
chrome.alarms.create("", {delayInMinutes: DELAY});
|
||||
browser.alarms.create("", {delayInMinutes: DELAY});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -56,15 +59,16 @@ function restartAlarm(tabId) {
|
||||
/*
|
||||
On alarm, show the page action.
|
||||
*/
|
||||
chrome.alarms.onAlarm.addListener(function(alarm) {
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
chrome.pageAction.show(tabs[0].id);
|
||||
browser.alarms.onAlarm.addListener((alarm) => {
|
||||
var gettingActiveTab = browser.tabs.query({active: true, currentWindow: true});
|
||||
gettingActiveTab.then((tabs) => {
|
||||
browser.pageAction.show(tabs[0].id);
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
On page action click, navigate the corresponding tab to the cat gifs.
|
||||
*/
|
||||
chrome.pageAction.onClicked.addListener(function () {
|
||||
chrome.tabs.update({url: CATGIFS});
|
||||
browser.pageAction.onClicked.addListener(function () {
|
||||
browser.tabs.update({url: CATGIFS});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user