Background: I'm trying to create google apps script with the following description: type: standalone webapp run as: me access: anyone
Files description
- Code.gs - Main GS Code
- Home.html - Main Page
- Content.html - Additional content
- Js.html - JavaScript code content
Everything works as expected
Problem: It concerns me that user can easily see the JavaScript code content from the HTML output (i.e. code is accessible from the browser web dev tool).
Main question:
- Is there a way to make JavaScript code content inaccessible by user
Possible related solution / additional question
I exclude it from the following snippets for now, since i couldn't get it to work, and might be off-topic (cmiiw)
(still, if it could be the solution for my problem, any further explanation / example of eval implementation using the snippets bellow would be greatly appreciated.)
- Separate the JavaScript into a library haven't really tried it yet because of possible performance issues or unnecessary complexity it might add since i don't intend for the JavaScript code to be used elsewhere other than within the single webapp i'm creating.
Sample
- A main html page ("Home") with some buttons
- on a button click it adds content from another html file ("Content")
- on another button click, it removes the added content
as it is, when user (me) checks the content of the webapp via web dev tool, the JavaScript code shows up inside the HTML output
Code.gs
function doGet(e) {
Logger.log(Utilities.jsonStringify(e) );
// html head
var output = HtmlService.createTemplateFromFile('Home').evaluate();
var output = output.asTemplate();
return output.evaluate()
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setTitle('Home');
}
function include(filename) {
return HtmlService.createTemplateFromFile(filename).evaluate().getContent();
}
function getHtmlOutput(filename) {
return HtmlService.createTemplateFromFile(filename).evaluate();
}
Home.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
div {
color: red;
}
</style>
</head>
<body>
<div id="content-container">
Hello World
<button type="button" onclick="add()">Add</button>
<button type="button" onclick="remove()">Remove</button>
<?!= include('js');?>
</div>
</body>
</html>
Content.html
<div id="content">
This is the content
</div>
Js.html
<script>
function runGS(functionName,params=[]) {
return new Promise((resolve,reject) => {
google.script.run
.withSuccessHandler(data => {
resolve(data)
})
.withFailureHandler(error => {
reject(error)
})
[functionName](...params);
})
}
async function add() {
var content = await runGS("include",["Content"]);
var target = document.getElementById("content-container");
target.insertAdjacentHTML("afterend",content);
return "done";
}
function remove() {
var target = document.getElementById("content");
target.remove();
}
</script>