Skip to content

Commit

Permalink
Merge pull request #6 from start-again/dev
Browse files Browse the repository at this point in the history
Close issue #5
  • Loading branch information
LucasCtrl authored Mar 23, 2020
2 parents cca52ca + 150211d commit f5ffb5c
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,9 @@ export default {
** Plugins to load before mounting the App
*/
plugins: [
'./plugins/HackTimer.js',
'./plugins/HackTimerWorker.js',
'./plugins/notificationRequest.js',
// './plugins/notificationEvent.js',
],
/*
** Nuxt.js dev-modules
Expand Down
155 changes: 155 additions & 0 deletions plugins/HackTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/* eslint-disable */
;(function (workerScript) {
if (!/MSIE 10/i.test(navigator.userAgent)) {
try {
var blob = new Blob([
"\
var fakeIdToId = {};\
onmessage = function (event) {\
var data = event.data,\
name = data.name,\
fakeId = data.fakeId,\
time;\
if(data.hasOwnProperty('time')) {\
time = data.time;\
}\
switch (name) {\
case 'setInterval':\
fakeIdToId[fakeId] = setInterval(function () {\
postMessage({fakeId: fakeId});\
}, time);\
break;\
case 'clearInterval':\
if (fakeIdToId.hasOwnProperty (fakeId)) {\
clearInterval(fakeIdToId[fakeId]);\
delete fakeIdToId[fakeId];\
}\
break;\
case 'setTimeout':\
fakeIdToId[fakeId] = setTimeout(function () {\
postMessage({fakeId: fakeId});\
if (fakeIdToId.hasOwnProperty (fakeId)) {\
delete fakeIdToId[fakeId];\
}\
}, time);\
break;\
case 'clearTimeout':\
if (fakeIdToId.hasOwnProperty (fakeId)) {\
clearTimeout(fakeIdToId[fakeId]);\
delete fakeIdToId[fakeId];\
}\
break;\
}\
}\
",
])
// Obtain a blob URL reference to our worker 'file'.
workerScript = window.URL.createObjectURL(blob)
} catch (error) {
/* Blob is not supported, use external script instead */
}
}
var worker,
fakeIdToCallback = {},
lastFakeId = 0,
maxFakeId = 0x7fffffff, // 2 ^ 31 - 1, 31 bit, positive values of signed 32 bit integer
logPrefix = 'HackTimer.js by turuslan: '
if (typeof Worker !== 'undefined') {
function getFakeId() {
do {
if (lastFakeId == maxFakeId) {
lastFakeId = 0
} else {
lastFakeId++
}
} while (fakeIdToCallback.hasOwnProperty(lastFakeId))
return lastFakeId
}
try {
worker = new Worker(workerScript)
window.setInterval = function (callback, time /* , parameters */) {
var fakeId = getFakeId()
fakeIdToCallback[fakeId] = {
callback: callback,
parameters: Array.prototype.slice.call(arguments, 2),
}
worker.postMessage({
name: 'setInterval',
fakeId: fakeId,
time: time,
})
return fakeId
}
window.clearInterval = function (fakeId) {
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
delete fakeIdToCallback[fakeId]
worker.postMessage({
name: 'clearInterval',
fakeId: fakeId,
})
}
}
window.setTimeout = function (callback, time /* , parameters */) {
var fakeId = getFakeId()
fakeIdToCallback[fakeId] = {
callback: callback,
parameters: Array.prototype.slice.call(arguments, 2),
isTimeout: true,
}
worker.postMessage({
name: 'setTimeout',
fakeId: fakeId,
time: time,
})
return fakeId
}
window.clearTimeout = function (fakeId) {
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
delete fakeIdToCallback[fakeId]
worker.postMessage({
name: 'clearTimeout',
fakeId: fakeId,
})
}
}
worker.onmessage = function (event) {
var data = event.data,
fakeId = data.fakeId,
request,
parameters,
callback
if (fakeIdToCallback.hasOwnProperty(fakeId)) {
request = fakeIdToCallback[fakeId]
callback = request.callback
parameters = request.parameters
if (request.hasOwnProperty('isTimeout') && request.isTimeout) {
delete fakeIdToCallback[fakeId]
}
}
if (typeof callback === 'string') {
try {
callback = new Function(callback)
} catch (error) {
console.log(
logPrefix + 'Error parsing callback code string: ',
error
)
}
}
if (typeof callback === 'function') {
callback.apply(window, parameters)
}
}
worker.onerror = function (event) {
console.log(event)
}
} catch (error) {
console.log(logPrefix + 'Initialisation failed')
console.error(error)
}
} else {
console.log(
logPrefix + 'Initialisation failed - HTML5 Web Worker is not supported'
)
}
})('HackTimerWorker.js')
38 changes: 38 additions & 0 deletions plugins/HackTimerWorker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* eslint-disable */
var fakeIdToId = {}
onmessage = function (event) {
var data = event.data,
name = data.name,
fakeId = data.fakeId,
time
if (data.hasOwnProperty('time')) {
time = data.time
}
switch (name) {
case 'setInterval':
fakeIdToId[fakeId] = setInterval(function () {
postMessage({ fakeId: fakeId })
}, time)
break
case 'clearInterval':
if (fakeIdToId.hasOwnProperty(fakeId)) {
clearInterval(fakeIdToId[fakeId])
delete fakeIdToId[fakeId]
}
break
case 'setTimeout':
fakeIdToId[fakeId] = setTimeout(function () {
postMessage({ fakeId: fakeId })
if (fakeIdToId.hasOwnProperty(fakeId)) {
delete fakeIdToId[fakeId]
}
}, time)
break
case 'clearTimeout':
if (fakeIdToId.hasOwnProperty(fakeId)) {
clearTimeout(fakeIdToId[fakeId])
delete fakeIdToId[fakeId]
}
break
}
}
2 changes: 1 addition & 1 deletion store/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const actions = {
icon: '/icon.png',
badge: '/badge.png',
}
navigator.serviceWorker.ready.then(function(sw) {
navigator.serviceWorker.ready.then(function (sw) {
sw.showNotification('PomStat', options)
})
},
Expand Down

0 comments on commit f5ffb5c

Please sign in to comment.