Skip to content

Commit

Permalink
Merge pull request #55 from NebulaServices/__devtesting
Browse files Browse the repository at this point in the history
BareSwitcher options module
  • Loading branch information
GreenyDEV committed Jan 3, 2023
2 parents e6ea7b3 + dd34702 commit 7f197e3
Show file tree
Hide file tree
Showing 10 changed files with 773 additions and 453 deletions.
1 change: 0 additions & 1 deletion static/arc-sw.js

This file was deleted.

3 changes: 2 additions & 1 deletion static/index.html

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion static/options/index.html

Large diffs are not rendered by default.

103 changes: 103 additions & 0 deletions static/resources/appModules/BareSwitcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// NOTE: THIS FILE MUST BE ACCOMPANIED BY THE ULTRAVIOLET BUNDLE BECAUSE IT CONTAINS THE INDEXEDDB LIBRARY

const dbPromise = Ultraviolet.openDB('keyval-store', 1, {
upgrade (db) {
db.createObjectStore('keyval')
}
})

function getBareLocation () {
return dbPromise
.then(db => db.get('keyval', 'bareLocation'))
.then(value => value || '')
}

self.storage = {
async get (key) {
return (await dbPromise).get('keyval', key)
},

async set (key, val) {
return (await dbPromise).put('keyval', val, key)
},

async del (key) {
return (await dbPromise).delete('keyval', key)
}
}

function setBareLocation (location) {
if (
/^http(s?):\/\//.test(location) ||
(location.includes('.') && val.substr(0, 1) !== ' ') ||
location.includes('/bare/')
) {
storage.set('bareLocation', location)
return 'Bare is located at: ' + location
} else {
console.log(
'Invalid Location provided, please provide a server in the format of http(s)://server.domain.com/'
)
return 'Invalid Location provided'
}
}

function bareValidator (bareLocation) {
try {
// open a request to the bare location
var xmlHttp = new XMLHttpRequest()
xmlHttp.open('GET', bareLocation, false) // false for synchronous request
xmlHttp.send(null)
const _response = xmlHttp.responseText
// turn the response text into json

const response = JSON.parse(_response)

if (response.project.name === 'bare-server-node') {
console.log('Bare located at: ' + bareLocation + '')
return true
} else {
console.error('Bare not found at: ' + bareLocation)
return false
}
} catch (error) {
console.error(
'An error occured while attempting to identify the bare server at: ' +
bareLocation
)
return false
}
}

window.addEventListener('load', () => {
console.log('Loaded ')
const _loc = document.getElementById('bareLocationInput')
const indicator = document.getElementById('validIndicator')

// wait 3 seconds
setTimeout(() => {
if (bareValidator(_loc.value) === true) {
indicator.innerText = 'Connected to server: ' + _loc.value
indicator.style.color = '#42f851'
} else if (bareValidator(_loc.value) === false) {
indicator.innerText = 'Could not connect to server: ' + _loc.value
indicator.style.color = '#f45145bd'
}
}, 1000)

document
.getElementById('bareLocationInput')
.addEventListener('keydown', function (event) {
if (event.key === 'Enter') {
if (bareValidator(_loc.value) === true) {
indicator.innerText = 'Connected to server: ' + _loc.value
indicator.style.color = '#42f851'
setBareLocation(_loc.value)
} else if (bareValidator(_loc.value) === false) {
_loc.value = ''
indicator.innerText = 'Could not connect to server: ' + _loc.value
indicator.style.color = '#f45145bd'
}
}
})
})
24 changes: 24 additions & 0 deletions static/resources/appModules/database-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// NOTE - This file is not used in the current version of the app.
// this is just a snippet of code that I am keeping for future reference.
// NOTE: THIS FILE MUST BE ACCOMPANIED BY THE ULTRAVIOLET BUNDLE BECAUSE IT CONTAINS THE INDEXEDDB LIBRARY


const dbPromise = Ultraviolet.openDB('keyval-store', 1, {
upgrade (db) {
db.createObjectStore('keyval')
}
})

self.storage = {
async get (key) {
return (await dbPromise).get('keyval', key)
},

async set (key, val) {
return (await dbPromise).put('keyval', val, key)
},

async del (key) {
return (await dbPromise).delete('keyval', key)
}
}
Loading

0 comments on commit 7f197e3

Please sign in to comment.