-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from 343max/1.0
1.0
- Loading branch information
Showing
9 changed files
with
242 additions
and
180 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
}) | ||
}) |
Oops, something went wrong.