Skip to content
This repository has been archived by the owner on Apr 4, 2021. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
cassus committed Jan 27, 2017
0 parents commit 703e025
Show file tree
Hide file tree
Showing 27 changed files with 4,184 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
"es2015"
]
}
35 changes: 35 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Dependency directory
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

build/

.DS_Store
.idea
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

Binary file added audio/solemn.mp3
Binary file not shown.
28 changes: 28 additions & 0 deletions event/src/aliases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const INITIATE_TRACKING = (orginalAction) => (dispatch) => {

const query = {currentWindow: true, active: true}

console.log('INITIATE_TRACKING')

chrome.tabs.query(query, (tabs) => {

console.log('tabs', tabs)

if (tabs.length !== 1) {
console.error('tabs.length !== 1', tabs.length)
}

dispatch({
type: 'START_TRACKING',
tab: tabs[0]
})
})

return {
type: 'INITIATE_TRACKING',
};
}

export default {
INITIATE_TRACKING
}
119 changes: 119 additions & 0 deletions event/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import {createStore, applyMiddleware} from 'redux'
import thunk from 'redux-thunk';
import {wrapStore, alias} from 'react-chrome-redux'

import rootReducer from './reducers'
import aliases from './aliases';


const middlewares = [
alias(aliases),
thunk
];

const store = createStore(rootReducer, applyMiddleware(...middlewares))

wrapStore(store, {
portName: 'port-6tbx4n2UxrcL'
})

const TIME_LIMIT = 10

function tabLoadedAction(tabId, changeInfo, tab) {
const lastActiveItem = store.getState()['activeItem']
const tracking = store.getState()['tracking']

return {
type: 'TAB_LOADED',
tabId,
changeInfo,
tab,
lastActiveItem,
tracking,
now: new Date()
};
}

function timeUpAction() {
const lastActiveItem = store.getState()['activeItem']

const myAudio = new Audio()
myAudio.src = "solemn.mp3"
myAudio.play()

return {
type: 'TIME_UP',
lastActiveItem,
now: new Date()
};
}


chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
// See https://developer.chrome.com/extensions/tabs#event-onUpdated

if (changeInfo.status !== 'complete') {
return
}

store.dispatch(tabLoadedAction(tabId, changeInfo, tab))
}
)

chrome.tabs.onActivated.addListener(({tabId, windowId}) => {
// console.log("onActivated", {tabId, windowId})
store.dispatch({
type: 'TAB_ACTIVATED',
tabId,
windowId,
})

})

store.subscribe(function updateIconOnStateChange() {
const {tracking, currentTab} = store.getState();

const path = (
tracking &&
currentTab.tabId === tracking.tabId &&
currentTab.windowId === tracking.windowId
? 'purple.png'
: 'grey.png'
)

chrome.browserAction.setIcon({path})
})

function updateBadge() {
const {activeItem, inboxItems} = store.getState()
const now = new Date()

if (!activeItem) {
return
}
const {url, loadedAt} = activeItem

if (!inboxItems[url]) {
return
}
const secondsSpentBefore = inboxItems[url].secondsSpent
const secondsSpentNow = Math.round((now.getTime() - loadedAt.getTime()) / 1000)


const timeSpent = (secondsSpentBefore + secondsSpentNow);
let badgeText = (timeSpent).toString() + "s"
chrome.browserAction.setBadgeText({text: badgeText})

if (TIME_LIMIT <= timeSpent) {
chrome.browserAction.setBadgeBackgroundColor({color: 'red'})
} else {
chrome.browserAction.setBadgeBackgroundColor({color: 'green'})
}


if (TIME_LIMIT <= timeSpent && timeSpent < TIME_LIMIT + 1) {
store.dispatch(timeUpAction())
}

}
setInterval(updateBadge, 1000)
22 changes: 22 additions & 0 deletions event/src/reducers/activeItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const initialState = null;

export default (state = initialState, action) => {
switch (action.type) {
case 'TAB_LOADED':
let {tabId, changeInfo, tracking, tab, now} = action

if (tracking && tracking.tabId === tab.id) {
// only care about URL changes from the tracked tab
let {url, title} = tab

state = {
loadedAt: now,
url
}
}

return state;
default:
return state;
}
};
20 changes: 20 additions & 0 deletions event/src/reducers/currentTab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const initialState = false;

export default (state = initialState, action) => {
switch (action.type) {
case 'START_TRACKING':
return {
tabId: action.tab.id,
windowId: action.tab.windowId,
};

case 'TAB_ACTIVATED':
return {
tabId: action.tabId,
windowId: action.windowId,
};

default:
return state;
}
};
67 changes: 67 additions & 0 deletions event/src/reducers/inboxItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const initialState = {}
const inboxItemDefaultState = {title: 'UNKNOWN', secondsSpent: 0}

function initializeInboxItemOnFirstOccurrence(state, url, title) {
if (!state[url]) {
state = {
...state,
[url]: {
...inboxItemDefaultState,
title,
},
}
}
return state;
}

export default (state = initialState, action) => {
switch (action.type) {
case 'START_TRACKING': {
const {url, title} = action.tab

state = initializeInboxItemOnFirstOccurrence(state, url, title);

return state;
}

case 'TAB_LOADED': {
const {tabId, changeInfo, tab, now, tracking, lastActiveItem:blurredItemInfo} = action
const {url, title} = tab

console.log('TAB_LOADED', {url, title})

state = initializeInboxItemOnFirstOccurrence(state, url, title);

if (tracking && tracking.tabId === tab.id) {

if (blurredItemInfo) {
const blurredUrl = blurredItemInfo.url

state = {
...state,
[blurredUrl]: blurredInboxItem(state[blurredUrl], blurredItemInfo, now),
}
}

console.log('state', state)
} else {
//console.log('ignored TAB_LOADED from non-tracked tab')
}
return state
}
default:
return state
}
}

function blurredInboxItem(state, blurredItemInfo, now) {
const blurredWasLoadedAt = blurredItemInfo.loadedAt

const secondsSpent = Math.round((now.getTime() - blurredWasLoadedAt.getTime()) / 1000)
const prevInboxItem = state || inboxItemDefaultState

return {
...prevInboxItem,
secondsSpent: prevInboxItem.secondsSpent + secondsSpent,
}
}
22 changes: 22 additions & 0 deletions event/src/reducers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {combineReducers} from 'redux';

import inboxItems from './inboxItems';
import activeItem from './activeItem';
import tracking from './tracking';
import currentTab from './currentTab';

export default combineReducers({
inboxItems,
activeItem,
tracking,
currentTab
});

//
// export default (state = {}, action) => {
//
// return {
// activeItem: activeItem(state.activeItem, action),
// inboxItems: inboxItems(state.inboxItems, action),
// }
// }
21 changes: 21 additions & 0 deletions event/src/reducers/tracking.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const initialState = false;

export default (state = initialState, action) => {
switch (action.type) {
case 'START_TRACKING':
console.log('START_TRACKING')

return {
tabId: action.tab.id,
windowId: action.tab.windowId,
};

case 'STOP_TRACKING':

console.log('STOP_TRACKING')

return false;
default:
return state;
}
};
33 changes: 33 additions & 0 deletions event/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const path = require('path');

module.exports = {

entry: [
'./event/src/index.js'
],

output: {
filename: 'event.js',
path: path.join(__dirname, '../', 'build')
},

resolve: {
extensions: ['', '.js', '.json'],
modulesDirectories: ['node_modules']
},

module: {
loaders: [
{
test: /\.(js)?$/,
loader: 'babel',
exclude: /(node_modules)/,
include: path.join(__dirname, 'src'),
query: {
presets: ['es2015', 'react'],
plugins: ['transform-object-rest-spread']
}
}
]
}
};
Loading

0 comments on commit 703e025

Please sign in to comment.