mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-16 06:18:35 +02:00
added page action example
This commit is contained in:
1
chill-out/README.md
Normal file
1
chill-out/README.md
Normal file
@@ -0,0 +1 @@
|
||||
# chill-out
|
||||
60
chill-out/background.js
Normal file
60
chill-out/background.js
Normal file
@@ -0,0 +1,60 @@
|
||||
var DELAY = 0.1;
|
||||
var CATGIFS = "http://chilloutandwatchsomecatgifs.com/";
|
||||
|
||||
/*
|
||||
Start-stop for the currently active tab, whenever this script is run.
|
||||
*/
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
startStopAlarm(tabs[0].id);
|
||||
});
|
||||
|
||||
/*
|
||||
Start-stop for the currently active tab, whenever the user navigates.
|
||||
*/
|
||||
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
|
||||
if (!changeInfo.url) {
|
||||
return;
|
||||
}
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
if (tabId == tabs[0].id) {
|
||||
startStopAlarm(tabId);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
Start-stop for the currently active tab, whenever a new tab becomes active.
|
||||
*/
|
||||
chrome.tabs.onActivated.addListener(function (activeInfo) {
|
||||
startStopAlarm(activeInfo.tabId);
|
||||
});
|
||||
|
||||
/*
|
||||
Start-stop: clear all alarms,
|
||||
then set a new alarm for the given tab.
|
||||
*/
|
||||
function startStopAlarm(tabId) {
|
||||
chrome.pageAction.hide(tabId);
|
||||
chrome.alarms.clearAll();
|
||||
chrome.tabs.get(tabId, function(tab) {
|
||||
if (tab.url != CATGIFS) {
|
||||
chrome.alarms.create(tabId, {delayInMinutes: DELAY});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
On alarm, show the page action.
|
||||
*/
|
||||
chrome.alarms.onAlarm.addListener(function(alarm) {
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
chrome.pageAction.show(alarm.name);
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
On page action click, navigate the corresponding tab to the cat gifs.
|
||||
*/
|
||||
chrome.pageAction.onClicked.addListener(function () {
|
||||
chrome.tabs.update({url: CATGIFS});
|
||||
});
|
||||
1
chill-out/button/LICENSE
Normal file
1
chill-out/button/LICENSE
Normal file
@@ -0,0 +1 @@
|
||||
The icon "chillout.png" is taken from the IconBeast Lite iconset, and used under the terms of its license (http://www.iconbeast.com/faq/), with a link back to the website: http://www.iconbeast.com/free/.
|
||||
BIN
chill-out/button/chillout.png
Normal file
BIN
chill-out/button/chillout.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 550 B |
26
chill-out/manifest.json
Normal file
26
chill-out/manifest.json
Normal file
@@ -0,0 +1,26 @@
|
||||
{
|
||||
|
||||
"manifest_version": 2,
|
||||
"name": "chillout-page-action",
|
||||
"version": "1.0",
|
||||
|
||||
"applications": {
|
||||
"gecko": {
|
||||
"id": "chillout-page-action@mozilla.org"
|
||||
}
|
||||
},
|
||||
|
||||
"permissions": [
|
||||
"tabs"
|
||||
],
|
||||
|
||||
"page_action": {
|
||||
"default_icon": "button/chillout.png",
|
||||
"default_title": "Chill out"
|
||||
},
|
||||
|
||||
"background": {
|
||||
"scripts": ["background.js"]
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user