commit 70730bbfad2d678caa7acfff2fb5f0f269eab85c Author: Lukas Winkler Date: Thu Aug 25 12:22:02 2016 +0200 initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9f11b75 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/icons/logo128.png b/icons/logo128.png new file mode 100644 index 0000000..b709784 Binary files /dev/null and b/icons/logo128.png differ diff --git a/icons/logo16.png b/icons/logo16.png new file mode 100644 index 0000000..1394b7a Binary files /dev/null and b/icons/logo16.png differ diff --git a/icons/logo180.png b/icons/logo180.png new file mode 100644 index 0000000..ab793e7 Binary files /dev/null and b/icons/logo180.png differ diff --git a/icons/logo38.png b/icons/logo38.png new file mode 100644 index 0000000..dd70910 Binary files /dev/null and b/icons/logo38.png differ diff --git a/icons/logo48.png b/icons/logo48.png new file mode 100644 index 0000000..75122dd Binary files /dev/null and b/icons/logo48.png differ diff --git a/main.js b/main.js new file mode 100644 index 0000000..2dc82f2 --- /dev/null +++ b/main.js @@ -0,0 +1,128 @@ +function getCounter() { + var x = new XMLHttpRequest(); + var formData = new FormData(); + formData.append("module", "API"); + formData.append("method", "Live.getCounters"); + formData.append("idSite", s.siteId); + formData.append("lastMinutes", s.badgelastHours * 60); + formData.append("format", "JSON"); + formData.append("token_auth", s.accessToken); + + x.open("POST", s.url + "index.php", true); + x.onreadystatechange = function () { + if (x.readyState == 4) { + if (x.status == 200) { + var response = JSON.parse(x.responseText)[0]; + chrome.browserAction.setBadgeText({text: response["visits"]}); + } else { + console.error("FEHLER:" + x.status + x.response) + } + } + }; + x.send(formData); +} +function getVisitorLog(success) { + var x = new XMLHttpRequest(); + var formData = new FormData(); + formData.append("module", "API"); + formData.append("method", "Live.getLastVisitsDetails"); + formData.append("period", "range"); + formData.append("date", "last" + s.listLastDays); + formData.append("filter_limit", s.listMax); + formData.append("idSite", s.siteId); + formData.append("format", "JSON"); + formData.append("token_auth", s.accessToken); + + x.open("POST", s.url + "index.php", true); + x.onreadystatechange = function () { + if (x.readyState == 4) { + if (x.status == 200) { + + var response = JSON.parse(x.responseText); + console.log(response); + success(response); + } else { + console.error("FEHLER:" + x.status + x.response) + } + + } + }; + x.send(formData); +} +function updateVisitorLog() { + document.getElementById('visitors').innerHTML = localStorage["htmlCache"]; + + getVisitorLog(function (response) { + var list = [ + "serverDatePretty", + "serverTimePretty", + "visitDurationPretty", + "browserIcon", + "deviceTypeIcon", + "operatingSystemIcon", + "visitorTypeIcon", + "countryFlag", + "actions", + "visitIp", + "referrerUrl" + ]; + document.getElementById('visitors').innerHTML = ""; + var i, visitor; + for (i = 0; i < response.length; ++i) { + visitor = response[i]; + var tr = document.createElement('tr'); + for (var j = 0; j < list.length; j++) { + var key = list[j]; + var td = tr.appendChild(document.createElement('td')); + if (key.indexOf("Icon") !== -1 || key.indexOf("Flag") !== -1) { + if (visitor[key]) { + td.innerHTML = ""; + } + } else if (key == "referrerUrl" && visitor[key]) { + var link = document.createElement('a'); + link.href = visitor[key]; + link.target = "_blank"; + link.textContent = link.hostname; + td.appendChild(link); + } + else { + td.textContent = visitor[key]; + } + tr.appendChild(td); + } + document.getElementById('visitors').appendChild(tr); + } + localStorage["htmlCache"] = document.getElementById('visitors').innerHTML; + return true; + }) +} +var s; +chrome.storage.local.get({ + url: "https://demo.piwik.org/", + siteId: 7, + accessToken: "", + badgelastHours: 1, + badgeRefresh: 1, + listMax: 10, + listLastDays: 2 + }, function (settings) { + s = settings; + if (chrome.extension.getBackgroundPage() === window) { + /* + chrome.runtime.onInstalled.addListener(function () { + chrome.tabs.create({ + url: chrome.extension.getURL("settings.html") + }); + }); + */ + + chrome.browserAction.setBadgeBackgroundColor({color: "#2c58a2"}); + getCounter(settings); + setInterval(getCounter, 60 * 1000); + + + } else { + updateVisitorLog(settings); + } + } +); diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..8eedc88 --- /dev/null +++ b/manifest.json @@ -0,0 +1,26 @@ +{ + "manifest_version": 2, + "icons": { + "16": "icons/logo16.png", + "48": "icons/logo48.png", + "128": "icons/logo128.png" + }, + "name": "Piwik Counter", + "description": "Description", + "version": "0.1", + "browser_action": { + "default_icon": "icons/logo38.png", + "default_popup": "popup.html" + }, + "background": { + "scripts": [ + "main.js" + ] + }, + "options_page": "settings.html", + "permissions": [ + "storage", + "http://*/*", + "https://*/*" + ] +} diff --git a/popup.html b/popup.html new file mode 100644 index 0000000..a94be71 --- /dev/null +++ b/popup.html @@ -0,0 +1,34 @@ + + + + + Piwik Popup + + + +
+ + + + + diff --git a/settings.html b/settings.html new file mode 100644 index 0000000..600c53f --- /dev/null +++ b/settings.html @@ -0,0 +1,46 @@ + + + + + Piwik Einstellungen + + +
+
+
+ + +
+
+ + +
+
+ + +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +
+ +
+
+ + + \ No newline at end of file diff --git a/settings.js b/settings.js new file mode 100644 index 0000000..741c259 --- /dev/null +++ b/settings.js @@ -0,0 +1,46 @@ +// Saves options to chrome.storage +function save_options(evt) { + evt.preventDefault(); + var status = document.getElementById('status'); + status.textContent = ''; + + chrome.storage.local.set({ + url: document.getElementById('url').value, + siteId: document.getElementById('siteId').value, + accessToken: document.getElementById('accessToken').value, + badgelastHours: document.getElementById('badgelastHours').value, + badgeRefresh: document.getElementById('badgeRefresh').value, + listMax: document.getElementById('listMax').value, + listLastDays: document.getElementById('listLastDays').value + }, function () { + // Update status to let user know options were saved. + console.log("saved"); + status.textContent = 'Options saved.'; + }); +} + +// Restores select box and checkbox state using the preferences +// stored in chrome.storage. +function restore_options() { + // Use default value color = 'red' and likesColor = true. + chrome.storage.local.get({ + url: "", + siteId: 1, + accessToken: "", + badgelastHours: 24, + badgeRefresh: 1, + listMax: 10, + listLastDays: 2 + }, function (settings) { + console.info(settings); + document.getElementById('url').value = settings.url; + document.getElementById('siteId').value = settings.siteId; + document.getElementById('accessToken').value = settings.accessToken; + document.getElementById('badgelastHours').value = settings.badgelastHours; + document.getElementById('badgeRefresh').value = settings.badgeRefresh; + document.getElementById('listMax').value = settings.listMax; + document.getElementById('listLastDays').value = settings.listLastDays; + }); +} +document.addEventListener('DOMContentLoaded', restore_options); +document.getElementById('form').addEventListener('submit', save_options);