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:
@@ -1,5 +1,5 @@
|
||||
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
|
||||
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||||
if (!tab.url.match(/^about:/)) {
|
||||
chrome.pageAction.show(tab.id);
|
||||
browser.pageAction.show(tab.id);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -8,7 +8,7 @@ function get_hostname(url) {
|
||||
|
||||
function set_domain(domain) {
|
||||
spans = document.getElementsByClassName('domain');
|
||||
[].slice.call(spans).forEach(function(span) {
|
||||
[].slice.call(spans).forEach((span) => {
|
||||
span.textContent = domain;
|
||||
});
|
||||
}
|
||||
@@ -20,52 +20,53 @@ function no_history(hostname) {
|
||||
history_text.textContent = `No history for ${hostname}.`;
|
||||
}
|
||||
|
||||
function getActiveTab() {
|
||||
return browser.tabs.query({active: true, currentWindow: true});
|
||||
}
|
||||
|
||||
// When the page is loaded find the current tab and then use that to query
|
||||
// the history.
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
getActiveTab().then((tabs) => {
|
||||
var list = document.getElementById('history');
|
||||
var hostname = get_hostname(tabs[0].url);
|
||||
|
||||
chrome.history.search(
|
||||
// Search for all history entries for the current windows domain.
|
||||
// Because this could be a lot of entries, lets limit it to 5.
|
||||
{text: hostname, maxResults: 5},
|
||||
function(results) {
|
||||
// What to show if there are no results.
|
||||
if (results.length < 1) {
|
||||
no_history(hostname);
|
||||
} else {
|
||||
for (var k in results) {
|
||||
var history = results[k];
|
||||
var li = document.createElement('p');
|
||||
var a = document.createElement('a');
|
||||
var url = document.createTextNode(history.url);
|
||||
a.href = history.url;
|
||||
a.target = '_blank';
|
||||
a.appendChild(url);
|
||||
li.appendChild(a);
|
||||
list.appendChild(li);
|
||||
}
|
||||
// Search for all history entries for the current windows domain.
|
||||
// Because this could be a lot of entries, lets limit it to 5.
|
||||
var searchingHistory = browser.history.search({text: hostname, maxResults: 5});
|
||||
searchingHistory.then((results) => {
|
||||
// What to show if there are no results.
|
||||
if (results.length < 1) {
|
||||
no_history(hostname);
|
||||
} else {
|
||||
for (var k in results) {
|
||||
var history = results[k];
|
||||
var li = document.createElement('p');
|
||||
var a = document.createElement('a');
|
||||
var url = document.createTextNode(history.url);
|
||||
a.href = history.url;
|
||||
a.target = '_blank';
|
||||
a.appendChild(url);
|
||||
li.appendChild(a);
|
||||
list.appendChild(li);
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
function clearAll(e) {
|
||||
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
|
||||
getActiveTab().then((tabs) => {
|
||||
var hostname = get_hostname(tabs[0].url);
|
||||
if (!hostname) {
|
||||
// Don't try and delete history when there's no hostname.
|
||||
return;
|
||||
}
|
||||
|
||||
chrome.history.search(
|
||||
{text: hostname},
|
||||
// Search will return us a list of histories for this domain.
|
||||
// Loop through them and delete them one by one.
|
||||
function(results) {
|
||||
// Search will return us a list of histories for this domain.
|
||||
// Loop through them and delete them one by one.
|
||||
var searchingHistory = browser.history.search({text: hostname})
|
||||
searchingHistory.then((results) => {
|
||||
for (k = 0; k < results.length; k++) {
|
||||
chrome.history.deleteUrl({url: results[k].url});
|
||||
browser.history.deleteUrl({url: results[k].url});
|
||||
}
|
||||
// Clear out the UI.
|
||||
no_history(hostname);
|
||||
|
||||
Reference in New Issue
Block a user