Added ability to update config from home page

main
Ben Busby 2020-04-05 17:59:50 -06:00
parent 9c0b4a7f58
commit 066c253c4d
3 changed files with 60 additions and 6 deletions

View File

@ -10,6 +10,9 @@ import urllib.parse as urlparse
from urllib.parse import parse_qs
from io import BytesIO
APP_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_FOLDER = os.path.join(APP_ROOT, 'static')
# Get Mozilla Firefox rhyme (important) and form a new user agent
mozilla = rhyme.get_rhyme('Mo') + 'zilla'
firefox = rhyme.get_rhyme('Fire') + 'fox'
@ -24,7 +27,7 @@ SEARCH_URL = 'https://www.google.com/search?gbv=1&q='
# (can be useful for achieving nojs on mobile)
nojs = int(os.environ.get('NOJS'))
config = json.load(open('config.json'))
user_config = json.load(open(STATIC_FOLDER + '/config.json'))
def get_ua(user_agent):
@ -81,8 +84,8 @@ def search():
# Grab city from config, if available
near = ''
if 'near' in config:
near = '&near=' + config['near']
if 'near' in user_config:
near = '&near=' + urlparse.quote(user_config['near'])
user_agent = request.headers.get('User-Agent')
full_query = q + tbs + tbm + start + near
@ -114,7 +117,7 @@ def search():
# Update logo
logo = soup.find('a', {'class': 'l'})
if logo is not None and 'Android' in user_agent or 'iPhone' in user_agent:
logo.insert(0, "Shoogle")
logo.insert(0, 'Shoogle')
logo['style'] = 'display: flex;justify-content: center;align-items: center;color: #685e79;font-size: 18px;'
# Replace hrefs with only the intended destination (no "utm" type tags)
@ -133,7 +136,7 @@ def search():
# Ensure no extra scripts passed through
try:
for script in soup("script"):
for script in soup('script'):
script.decompose()
soup.find('div', id='sfooter').decompose()
except Exception:
@ -142,6 +145,18 @@ def search():
return render_template('display.html', response=soup)
@app.route('/config', methods=['POST'])
def config():
global user_config
with open(STATIC_FOLDER + '/config.json', 'w') as config_file:
config_file.write(json.dumps(json.loads(request.data), indent=4))
config_file.close()
user_config = json.loads(request.data)
return 'New config: ' + str(request.data)
@app.route('/url', methods=['GET'])
def url():
if 'url' in request.args:

View File

@ -1,4 +1,6 @@
document.addEventListener("DOMContentLoaded", function() {
// Setup search field
const searchBar = document.getElementById("search-bar");
const searchBtn = document.getElementById("search-submit");
@ -15,5 +17,36 @@ document.addEventListener("DOMContentLoaded", function() {
searchBtn.onclick = function() {
window.location.href = '/search?q=' + encodeURI(searchBar.value);
};
// Setup shoogle config
const saveConfig = document.getElementById("config-submit");
const nearConfig = document.getElementById("config-near");
// Request existing config info
var xhr = new XMLHttpRequest();
xhr.open("GET", "/static/config.json");
xhr.onload = function() {
if (xhr.readyState === 4 && xhr.status !== 200) {
alert("Error loading Shoogle config");
return;
}
// Allow for updating/saving config values
let configSettings = JSON.parse(xhr.responseText);
nearConfig.value = configSettings["near"];
nearConfig.addEventListener("keyup", function(event) {
configSettings["near"] = nearConfig.value;
});
saveConfig.onclick = function() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/config");
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.send(JSON.stringify(configSettings));
}
};
xhr.send();
});

View File

@ -10,4 +10,10 @@
<input type="text" id="search-bar">
<button type="submit" id="search-submit">Search</button>
</div>
<br/>
<div class="config-fields">
<label for="config-near">Near: </label>
<input type="text" name="config-near" id="config-near" placeholder="City Name">
<button type="submit" id="config-submit">Save</button>
</div>
</div>