Skip to content

Commit

Permalink
Merge pull request #25 from 343max/1.0
Browse files Browse the repository at this point in the history
1.0
  • Loading branch information
HorayNarea authored Apr 21, 2018
2 parents 3e0b57d + 6f7c2d9 commit 43f58ee
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 180 deletions.
76 changes: 0 additions & 76 deletions .eslintrc.json

This file was deleted.

88 changes: 88 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
extends: eslint:recommended

env:
es6: true,
browser: true,
webextensions: true

rules:
# warnings
no-control-regex: warn
no-ternary: warn
prefer-const: warn
prefer-template: warn
no-magic-numbers:
- warn
- ignoreArrayIndexes: true
# errors
yoda: error
radix: error
no-useless-return: error
no-useless-concat: error
no-unused-expressions: error
no-self-compare: error
no-script-url: error
no-return-assign: error
no-octal-escape: error
no-loop-func: error
no-lone-blocks: error
no-invalid-this: error
no-implied-eval: error
no-floating-decimal: error
no-eval: error
no-eq-null: error
no-empty-function: error
no-else-return: error
no-alert: error
eqeqeq: error
dot-notation: error
no-shadow: error
no-undef-init: error
no-use-before-define: error
brace-style: error
comma-dangle: error
comma-spacing: error
eol-last: error
func-call-spacing: error
keyword-spacing: error
linebreak-style: error
no-bitwise: error
no-lonely-if: error
no-multi-assign: error
no-multiple-empty-lines: error
no-nested-ternary: error
no-trailing-spaces: error
semi-spacing: error
space-before-blocks: error
space-in-parens: error
space-infix-ops: error
switch-colon-spacing: error
wrap-regex: error
no-confusing-arrow: error
no-useless-computed-key: error
no-var: error
template-curly-spacing: error
semi:
- error
- never
no-extra-parens:
- error
- all
valid-typeof:
- error
- requireStringLiterals: true
strict:
- error
- global
curly:
- error
- multi-line
indent:
- error
- tab
quotes:
- error
- single
space-before-function-paren:
- error
- never
10 changes: 9 additions & 1 deletion Chrome/_locales/de/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
"message": "Eine URL pro Zeile!",
"description": "Zweiter fett formatierter Satz auf der Einstellungsseite"
},
"optionsAudible": {
"message": "Tabs die Audio spielen wach halten",
"description": "Checkbox für die Audio-Einstellung"
},
"optionsThumbnails": {
"message": "Screenshot von manuell schlafen gelegten Tabs anzeigen (experimentell!)",
"description": "Checkbox für die Thumbnail-Einstellung"
},
"optionsSaveButton": {
"message": "Speichern",
"description": "Text des Speichern-Buttons"
Expand All @@ -63,4 +71,4 @@
"message": "Aufwecken",
"description": "Text des Aufweck-Buttons einer inaktiven Seite"
}
}
}
10 changes: 9 additions & 1 deletion Chrome/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
"message": "One URL per line!",
"description": "options page second bold sentence"
},
"optionsAudible": {
"message": "keep audio-playing tabs awake",
"description": "checkbox for the audio-option"
},
"optionsThumbnails": {
"message": "show screenshot of manually hibernated tabs (experimental!)",
"description": "checkbox for the thumbnail-option"
},
"optionsSaveButton": {
"message": "Save",
"description": "save button text"
Expand All @@ -63,4 +71,4 @@
"message": "Wake up!",
"description": "text of the button to wake up a hibernating page"
}
}
}
133 changes: 77 additions & 56 deletions Chrome/eventPage.js
Original file line number Diff line number Diff line change
@@ -1,91 +1,112 @@
'use strict'

var whitelist
var whitelistArray = new Array()
let whitelistArray = new Array()
let caffeinatedAudio = false
let thumbnails = false

chrome.storage.sync.get(function(items) {
if (items.whitelist) {
whitelist = items.whitelist
whitelistArray = whitelist.split('\n')
if (items.whitelist) whitelistArray = items.whitelist.split('\n')
if (items.audible !== undefined) caffeinatedAudio = items.audible
if (items.thumbnails !== undefined) thumbnails = items.thumbnails
})

chrome.storage.onChanged.addListener(function(changes, area) {
if (area === 'sync') {
chrome.storage.sync.get(function(items) {
if (items.whitelist) whitelistArray = items.whitelist.split('\n')
if (items.audible !== undefined) caffeinatedAudio = items.audible
if (items.thumbnails !== undefined) thumbnails = items.thumbnails
})
}
})

chrome.runtime.onInstalled.addListener(function() {
chrome.contextMenus.create({
id : 'SleepTab',
title : chrome.i18n.getMessage('contextMenuTitle'),
contexts : ['page']
})
})

function inWhitelist(url) {
var listed = false
whitelistArray.forEach(function(item) {
if (url.startsWith(item)) {
listed = true
return true
}
})
return listed
return false
}

function sleepTab(html, tab) {
var pageInfo = {
function sleepTab(html, tab, img) {
const pageInfo = {
url: tab.url,
title: tab.title,
hibernationInfo: chrome.i18n.getMessage('hibernationPageInfo'),
buttonText: chrome.i18n.getMessage('hibernationPageButton'),
favIconUrl: tab.favIconUrl
}
var pageHtml = html.replace(/\{\/\*pageInfoObject\*\/\}/, JSON.stringify(pageInfo))
var dataURL = 'data:text/html;charset=utf-8,' + encodeURIComponent(pageHtml)
chrome.tabs.update(tab.id, {url: dataURL})
}

chrome.browserAction.onClicked.addListener(function(tab) {
var c = 0
const pageHtml = html.replace(/\{\/\*pageInfoObject\*\/\}/, JSON.stringify(pageInfo))

var xmlHttp = new XMLHttpRequest()
xmlHttp.open('GET', chrome.runtime.getURL('hibernationPage/index.html'), true)
xmlHttp.onload = function () {
var html = xmlHttp.responseText
if (img && thumbnails) {
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')

chrome.windows.getAll({populate: true}, function(windows) {
windows.forEach(function(win) {
win.tabs.forEach(function(tab) {
if (tab.active || tab.highlighted || tab.pinned) return
if (tab.status !== 'complete') return
if (!tab.url.match(/^https?:\/\//)) return
if (inWhitelist(tab.url)) return
const i = new Image()
i.src = img
i.onload = function() {
canvas.width = 300
canvas.height = canvas.width * (i.height / i.width)
ctx.drawImage(i, 0, 0, canvas.width, canvas.height)
img = `<img id="screenshot" src="${canvas.toDataURL('image/webp')}"/>`

sleepTab(html, tab)
c++
})
})
const dataURL = `data:text/html;charset=UTF-8,${encodeURIComponent(pageHtml.replace(/<!-- SCREENSHOT_IF_ENABLED -->/, img))}`
chrome.tabs.update(tab.id, {url: dataURL, autoDiscardable: true})
}
return
}
const dataURL = `data:text/html;charset=UTF-8,${encodeURIComponent(pageHtml)}`
chrome.tabs.update(tab.id, {url: dataURL, autoDiscardable: true})
}

function sleepSingle(tab) {
const xmlHttp = new XMLHttpRequest()
xmlHttp.open('GET', chrome.runtime.getURL('hibernationPage/index.html'), true)
xmlHttp.onload = function() {
chrome.tabs.captureVisibleTab({format: 'png'}, function(img) {
sleepTab(xmlHttp.responseText, tab, img)
})
}
xmlHttp.send(null)
})

const onInstalledDetails = {details: {'OnInstalledReason': 'installed'}}

chrome.runtime.onInstalled.addListener(function(onInstalledDetails) {
chrome.contextMenus.create({
id : 'SleepTab',
title : chrome.i18n.getMessage('contextMenuTitle'),
contexts : ['page']
})
})
}

chrome.contextMenus.onClicked.addListener(function(info, tab) {
var xmlHttp = new XMLHttpRequest()
chrome.browserAction.onClicked.addListener(function() {
const xmlHttp = new XMLHttpRequest()
xmlHttp.open('GET', chrome.runtime.getURL('hibernationPage/index.html'), true)
xmlHttp.onload = function () {
var html = xmlHttp.responseText
sleepTab(html, tab)
xmlHttp.onload = function() {
chrome.tabs.query({
active: false,
pinned: false,
highlighted: false,
status: 'complete',
url: ['http://*/*', 'https://*/*', 'ftp://*/*', 'file://*/*']
}, function(tabs) {
tabs.forEach(function(tab) {
if (inWhitelist(tab.url)) return
if (tab.audible && caffeinatedAudio) return
sleepTab(xmlHttp.responseText, tab)
})
})
}
xmlHttp.send(null)
})

chrome.commands.onCommand.addListener(function(command) {
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tab) {
var xmlHttp = new XMLHttpRequest()
xmlHttp.open('GET', chrome.runtime.getURL('hibernationPage/index.html'), true)
xmlHttp.onload = function () {
var html = xmlHttp.responseText
sleepTab(html, tab[0])
}
xmlHttp.send(null)
chrome.contextMenus.onClicked.addListener(function(info, tab) {
sleepSingle(tab)
})

chrome.commands.onCommand.addListener(function() {
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs) {
sleepSingle(tabs[0])
})
})
Loading

0 comments on commit 43f58ee

Please sign in to comment.