From e6bbf3d41b57c34eb5e36fa5e85b71de39be9c8e Mon Sep 17 00:00:00 2001 From: Andy McKay Date: Mon, 6 Jun 2016 16:57:30 -0700 Subject: [PATCH] provide an example using the history api --- history-deleter/README.md | 11 ++++++ history-deleter/background.js | 5 +++ history-deleter/history.css | 4 +++ history-deleter/history.html | 16 +++++++++ history-deleter/history.js | 63 +++++++++++++++++++++++++++++++++++ history-deleter/manifest.json | 24 +++++++++++++ 6 files changed, 123 insertions(+) create mode 100644 history-deleter/README.md create mode 100644 history-deleter/background.js create mode 100644 history-deleter/history.css create mode 100644 history-deleter/history.html create mode 100644 history-deleter/history.js create mode 100644 history-deleter/manifest.json diff --git a/history-deleter/README.md b/history-deleter/README.md new file mode 100644 index 0000000..4c2d3b0 --- /dev/null +++ b/history-deleter/README.md @@ -0,0 +1,11 @@ +# History deleter + +## What it does + +This extension includes a page action with a popup specified as "history.html". The page action will not appear on about:... pages. + +The popup shows a list of 5 history entries for the current domain. It provides a clear button to delete all entries for that domain. + +## What it shows + +How to use the history API. diff --git a/history-deleter/background.js b/history-deleter/background.js new file mode 100644 index 0000000..db6433c --- /dev/null +++ b/history-deleter/background.js @@ -0,0 +1,5 @@ +chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { + if (!tab.url.match(/^about:/)) { + chrome.pageAction.show(tab.id); + } +}); diff --git a/history-deleter/history.css b/history-deleter/history.css new file mode 100644 index 0000000..b789105 --- /dev/null +++ b/history-deleter/history.css @@ -0,0 +1,4 @@ +html, body { + margin: 0.2em; + width: 350px; +} diff --git a/history-deleter/history.html b/history-deleter/history.html new file mode 100644 index 0000000..5fc5382 --- /dev/null +++ b/history-deleter/history.html @@ -0,0 +1,16 @@ + + + + + + + + + +

History for this domain (limited to 5 results):

+

+

Clear history for this domain

+ + + + diff --git a/history-deleter/history.js b/history-deleter/history.js new file mode 100644 index 0000000..6e5c31e --- /dev/null +++ b/history-deleter/history.js @@ -0,0 +1,63 @@ +// A useful way to extract the domain from a url. +function get_hostname(url) { + var a = document.createElement('a'); + a.href = url; + return a.hostname; +} + +function no_history(hostname) { + document.getElementById('history').innerHTML = `No history for ${hostname}.`; +} + +// 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) { + 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. + {text: hostname, maxResults: 5}, + function(results) { + // What to show if there are no results. + if (results.length < 1) { + no_history(hostname); + } else { + // Because this could be a lot of entries, lets limit it to 5. + for (var k in results) { + var history = results[k]; + var li = document.createElement('p'); + var url = document.createTextNode(history.url); + li.appendChild(url); + list.appendChild(li); + } + } + } + ); +}); + +function clearAll(e) { + chrome.tabs.query({active: true, currentWindow: true}, function(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) { + for (k = 0; k < results.length; k++) { + chrome.history.deleteUrl({url: results[k].url}); + } + // Clear out the UI. + no_history(hostname); + } + ); + }); + e.preventDefault(); +} + +document.getElementById('clear').addEventListener('click', clearAll); diff --git a/history-deleter/manifest.json b/history-deleter/manifest.json new file mode 100644 index 0000000..22e7537 --- /dev/null +++ b/history-deleter/manifest.json @@ -0,0 +1,24 @@ +{ + "applications": { + "gecko": { + "id": "history-deleter@mozilla.com" + } + }, + "background": { + "scripts": ["background.js"] + }, + "description": "Gives a popup to list and delete history on a domain.", + "homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/history-deleter", + "page_action": { + "default_title": "History deleter", + "default_popup": "history.html" + }, + "permissions": [ + "activeTab", + "history", + "tabs" + ], + "manifest_version": 2, + "name": "History Deleter", + "version": "1.0" +}