Skip to content

Instantly share code, notes, and snippets.

@0x09
Created February 19, 2017 07:15
Show Gist options
  • Save 0x09/d1220a7b0a0bda64b4f542067cd43ec7 to your computer and use it in GitHub Desktop.
Save 0x09/d1220a7b0a0bda64b4f542067cd43ec7 to your computer and use it in GitHub Desktop.
<html>
<script>
var db = openDatabase('watch','1.0','watch',104857600);
db.transaction(function(t) {
t.executeSql("CREATE TABLE IF NOT EXISTS events(time DATETIME, duration INTEGER, url TEXT);", []);
});
var event = null;
function eventclose(e) {
var tab = e.target;
if(tab.toString() === '[object SafariBrowserWindow]')
tab = tab.activeTab;
if(event && (e.target === safari.application.activeBrowserWindow || e.target === safari.application.activeBrowserWindow.activeTab || tab === event.tab)) {
db.transaction(function(t) {
t.executeSql("INSERT INTO events VALUES (?,?,?)", [event.ts/1000, e.timeStamp-event.ts, event.url]);
});
event = null;
}
}
function eventopen(e) {
var tab = e.target;
if(tab.toString() === '[object SafariBrowserWindow]')
tab = tab.activeTab;
if((e.type === 'activate' || tab === safari.application.activeBrowserWindow.activeTab) && (!event || tab.url != event.url)) {
eventclose(e);
if(tab.url && !safari.application.privateBrowsing.enabled)
event = {ts: e.timeStamp, url: tab.url, tab: tab};
}
}
safari.application.addEventListener("activate", eventopen, true);
safari.application.addEventListener("navigate", eventopen, true);
safari.application.addEventListener("open", eventopen, true);
safari.application.addEventListener("deactivate", eventclose, true);
safari.application.addEventListener("close", eventclose, true);
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment