changeset 7:0472897e790d

add javascript
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 23 Jun 2022 23:38:03 -0600
parents 9166f6a14021
children be36282b556a
files src/error_log.js.luan src/lib/Shared.luan src/site.js
diffstat 3 files changed, 71 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/error_log.js.luan	Thu Jun 23 23:38:03 2022 -0600
@@ -0,0 +1,36 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local String = require "luan:String.luan"
+local matches = String.matches or error()
+local trim = String.trim or error()
+local Table = require "luan:Table.luan"
+local Http = require "luan:http/Http.luan"
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "error_log"
+
+--[=[ when needed
+local bad_bots = {
+	[[Googlebot]]
+	[[bingbot]]
+}
+local bad_bots_ptn = Table.concat(bad_bots,"|")
+]=]
+
+local function priority(err)
+--[=[
+	local agent = Http.request.headers["user-agent"]
+	if agent~=nil and matches(agent,bad_bots_ptn) then return "info" end
+	if matches(err,[[Uncaught TypeError: Illegal invocation]]) then return "warn" end
+	if matches(err,[[\Q?fbclid=\E]]) then return "warn" end
+]=]
+	return "error"
+end
+
+return function()
+	local err = Http.request.parameters.err
+	if err == nil then
+		return  -- stupid bots
+	end
+	local call = priority(err)
+	logger[call](trim(err).."\n"..trim(Http.request.raw_head).."\n")
+end
--- a/src/lib/Shared.luan	Thu Jun 23 23:05:28 2022 -0600
+++ b/src/lib/Shared.luan	Thu Jun 23 23:38:03 2022 -0600
@@ -21,12 +21,14 @@
 		<style>
 			@import "/site.css";
 		</style>
+		<script src="/site.js"></script>
 <%
 end
 
 function Shared.header()
 	local user = User.current()
 %>
+		<p style="text-align:center">Yes the aesthetics suck.  Will be fixed later.</p>
 		<div header>
 			<a href="/"><%=forum_title%></a>
 <%	if user == nil then %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/site.js	Thu Jun 23 23:38:03 2022 -0600
@@ -0,0 +1,33 @@
+
+function ajax(url,postData) {
+	let request = new XMLHttpRequest();
+	let method = postData ? 'POST' : 'GET';
+	request.open( method, url );
+	if( postData )
+		request.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
+	request.onload = function() {
+		if( request.status !== 200 ) {
+			window.console && console.log( 'ajax failed: ' + request.status );
+			if( request.responseText )
+				document.write('<pre>'+request.responseText+'</pre>');
+			return;
+		}
+		eval( request.responseText );
+	};
+	request.send(postData);
+}
+
+window.onerror = function(msg, url, line, col, error) {
+	if (!url)
+		return;
+	let err = msg;
+	err += '\nurl = ' + url;
+	if (url != window.location)
+		err += '\npage = ' + window.location;
+	err += '\nline = '+line;
+	if (col)
+		err += '\ncolumn = ' + col;
+	if (error && error.stack)
+		err += '\nstack = ' + error.stack;
+	ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
+};