a tabs example

This commit is contained in:
Andy McKay
2016-02-12 16:47:21 -08:00
parent 8cfcdb7696
commit 46263764a0
4 changed files with 84 additions and 0 deletions

8
tabs-tabs-tabs/README.md Normal file
View File

@@ -0,0 +1,8 @@
# tabs, tabs, tabs
This extension includes:
* a browser action
* a page "tabs.html" with a script "tabs.js"
It shows a list of tab methods that you can perform on the active tab.

View File

@@ -0,0 +1,17 @@
{
"manifest_version": 2,
"name": "Tabs, tabs, tabs",
"version": "0.1",
"applications": {
"gecko": {
"id": "tabs-tabs-tabs@mozilla.org"
}
},
"browser_action": {
"default_title": "Tabs, tabs, tabs",
"default_popup": "tabs.html"
},
"permissions": [
"tabs"
]
}

17
tabs-tabs-tabs/tabs.html Normal file
View File

@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<a href="#" id="tabs-move-beginning">Move active tab to the beginning of the window</a><br>
<a href="#" id="tabs-move-end">Move active tab to the end of the window</a><br>
<a href="#" id="tabs-duplicate">Duplicate active tab</a><br>
<a href="#" id="tabs-reload">Reload active tab</a><br>
<a href="#" id="tabs-remove">Remove active tab</a><br>
<script src="tabs.js"></script>
</body>
</html>

42
tabs-tabs-tabs/tabs.js Normal file
View File

@@ -0,0 +1,42 @@
document.addEventListener("click", function(e) {
function callOnActiveTab(callback) {
chrome.tabs.query({}, function(tabs) {
for (var tab of tabs) {
if (tab.active) {
callback(tab);
}
}
});
}
if (e.target.id === "tabs-move-beginning") {
callOnActiveTab((tab) => {
chrome.tabs.move([tab.id], {index: 0});
});
}
if (e.target.id === "tabs-move-end") {
callOnActiveTab((tab) => {
chrome.tabs.move([tab.id], {index: -1});
});
}
else if (e.target.id === "tabs-duplicate") {
callOnActiveTab((tab) => {
chrome.tabs.duplicate(tab.id);
});
}
else if (e.target.id === "tabs-reload") {
callOnActiveTab((tab) => {
chrome.tabs.reload(tab.id);
});
}
else if (e.target.id === "tabs-remove") {
callOnActiveTab((tab) => {
chrome.tabs.remove(tab.id);
});
}
});