Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BareSwitcher options module #55

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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