sample code of ejs.render inside script tag not working it gives error as variable is not defined - ejs

<script src="ejs.js"></script>
<script>
let people = ['geddy', 'neil', 'alex'];
let html = ejs.render('<%= people.join(", "); %>', {people: people});
</script>
This code is in official docs of ejs, but when used in ejs file I get error
people is not defined
How do I resolve this??

Can you please confirm that you're using browser version of Ejs? I copied the same code and it is working for me.
Here is the jsfiddle and it console out the result without error.
let people = ['geddy', 'neil', 'alex'];
let html = ejs.render('<%= people.join(", "); %>', {people: people});
console.log(html);

Related

How do I open a game compiled for WebGL in Unity in Blazor?

I have created a Blazor Server project. In it, I wanted to put my WebGL game created in Unity3d on a separate page. In the end, after doing everything according to the example, I still can't get it to work. Although, I think, all things considered, but I see that the code markup index.html from WebGL game is different from other examples and there I haven't found a line of code:
unityInstance = UnityLoader.instantiate("unityContainer", "unity/WebGL/Build/WebGL.loader.js", { onProgress: UnityProgress });
I think I hooked it up right:
<script src="~/unity/WebGL/Build/WebGL.loader.js"></script>
#*<script src="~/unity/WebGL/Build/WebGL.framework.js.gz"></script>*#
<script src="~/unity/WebGL/run.js"></script>
I believe I wrote the mime type correctly:
var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Remove(".data.gz");
provider.Mappings[".data.gz"] = "application/octet-stream";
provider.Mappings.Remove(".wasm.gz");
provider.Mappings[".wasm.gz"] = "application/wasm";
provider.Mappings.Remove(".js.gz");
provider.Mappings[".js.gz"] = "application/javascript";
provider.Mappings.Remove(".symbols.json.gz");
provider.Mappings[".symbols.json.gz"] = "application/octet-stream";
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "unity", "WebGL", "Build")),
RequestPath = "/Build",
ContentTypeProvider = provider
});
app.UseStaticFiles();
The error in the JavaScript console reads:
UnityLoader is not defined
If interested, here is my entire project:
https://github.com/EgorPavlovich/FPS.Servers.Test
I had to use the index.html file from the game build

How do I embed a Facebook Feed in the new Google Sites (using Apps Script)?

I've read that you can make a Google Apps Script that shows a Facebook Feed, and then embed this in a Google Site, but I can't find any more information on how to do it and I can't figure it out myself.
When I try to make an Apps Script web app with a Facebook feed I get errors like:
Uncaught DOMException: Failed to set the 'domain' property on 'Document': Assignment is forbidden for sandboxed iframes.
This is from copying the "Facebook Javascript SDK" and "Page Feed" from Facebook Developers into an HTML file and deploying it as a web app. I gather it has something to do with how Apps Script sandboxes your code but I don't know what I have to do here.
For that matter, even if I try to make a simpler Apps Script with some static HTML, when I try to embed it from Drive into the site I get an error "Some of the selected items could not be embedded".
The New Google Sites doesn't support Google Apps Script.
Related question: Google App Scripts For New Google Sites Release
The new Google Sites does now support embedding apps script (make sure to deploy the apps script as a web app, set the right permissions, and use the /exec url and not your /dev one to embed).
I found I couldn't use the facebook SDK for videos because of the sandboxing. I used an iframe solution instead for videos, but maybe you could try something like this for the feed (I'm assuming you've registered your app in fb so you can get generate tokens):
In apps script, create a .gs file and an html file, roughly along the lines below (I haven't actually worked with returning feeds, so check the returned data structure and adjust accordingly)
//**feed.gs**
function doGet(e) {
return HtmlService
.createTemplateFromFile('my-html-file')
.evaluate();
}
function getToken() { //use your fb app info here (and make sure this script is protected / runs as you
var url = 'https://graph.facebook.com'
+ '/oauth/access_token'
+ '?client_id=0000000000000000'
+ '&client_secret=0x0x0x0x0x0x0x0x0x0x0x0x'
+ '&grant_type=client_credentials';
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var jsondata = JSON.parse(json);
return jsondata.access_token;
}
function getFeed() {
var url = 'https://graph.facebook.com'
+ '/your-page/feed'
+ '?access_token=' + encodeURIComponent(getToken());
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
var json = response.getContentText();
var jsondata = JSON.parse(json);
//Logger.log(jsondata); //check this and adjust following for loop and html showFeed function accordingly
var posts = {};
for (var i in jsondata) {
posts[i] = {"post":jsondata[i].message};
}
return posts;
}
<!--**my-html-file.html**-->
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
// The code in this function runs when the page is loaded (asynchronous).
$(function() {
google.script.run
.withSuccessHandler(showFeed)
.withFailureHandler(onFailure)
.getFeed(); //this function is back in .gs file and must return an array or object which gets auto-passed to the showFeed function below
});
function showFeed(posts) { //parameter name must match array or object returned by getFeed in gs file
var html = '';
for (var p in posts) {
html += '<p>' + posts[p].post + '</p>'; //instead of a string, you can build an array for speed
}
$('#feed').empty().append(html); //if you used an array for the html, you'd split it here
}
function onFailure(error) {
$('#feed').empty().append("Unable to retrieve feed: " + error.message); ;
}
</script>
</head>
<body>
<div id="feed">
Loading...
</div>
</body>
</html>

why my Riot JS update isn't working?

So I'm learning Riot JS, following a guide. Gives an example explaining step by step.
And adds a "this.update()" to update the riot js variables. Now, it is working for him, but not for me. Can you guys tell me why?
Here's the code.
This is the index.html
<body>
<script src="bower_components/riot/riot.min.js" type="text/javascript"></script>
<script src="tags/all.js" type="text/javascript"></script>
<contact-list></contact-list>
<script>
riot.mount('contact-list', {callback: tagCallback});
function tagCallback(theTag) {
console.log('callback executed');
var request = new XMLHttpRequest();
request.open('GET', 'people.json', true);
request.onload = function() {
if(request.status == 200) {
var data = JSON.parse(request.responseText);
console.log(data);
theTag.trigger('data_loaded', data);
}
}
setTimeout(function() {
request.send();
},2000);
}
</script>
</body>
And this is my contact-list.tag
<contact-list>
<h1>Contacts</h1>
<ul>
<li each={p in opts.people}>{p.first} {p.last}</li>
</ul>
<script>
this.on('mount', function() {
console.log('Riot mount event fired');
opts.callback(this);
})
this.on('data_loaded', function(peeps) {
console.log(peeps);
opts.people = peeps;
this.update();
})
</script>
</contact-list>
After debugging with the console.logs I can see i'm retrieving data correctly from my JSON file, my contact list data is there. But the bullet list isn't updated. It's displayed empty.
Is there any reason for using a callback function?
If not, move your callback function into the tag and update it directly after assigning your fetched data to your tags variable.
Look at the sources in riotgear:
https://github.com/RiotGear/rg/blob/master/tags/rg-include/rg-include.tag
For me it was a perfect example.
Oh nevermind guys, sorry. Don't know how actually the example of the guy in the video worked. Because I had to pass data.people on the html trigger event. Otherwise i was passing a plane Object with an Array in it.

karma-runner: load scripts (and CSS) via DOM

I want to inject some CSS and JavaScript files via a preprocessor.
In my preprocessor I inject the html template to the body element.
I printed the result out via console.log(document.body) - you can see the result at the bottom. It looks good, but the script is not evaluated.
If I run console.log(window.foobar) in my test, it's undefined.
Actually I don't want to to inject simple scripts, I want to load some files via
<script src="build/app.js"></script>
I need it in every test, so I don't want to refactor every single test for the same code injection, that's the reason why I tried to put it into the html generated by karma.
<body><script> window.foobar = 'miau!';</script>
<!-- The scripts need to be at the end of body, so that some test running frameworks
(Angular Scenario, for example) need the body to be loaded so that it can insert its magic
into it. If it is before body, then it fails to find the body and crashes and burns in an epic
manner. -->
<script type="text/javascript">
// sets window.__karma__ and overrides console and error handling
// Use window.opener if this was opened by someone else - in a new window
if (window.opener) {
window.opener.karma.setupContext(window);
} else {
window.parent.karma.setupContext(window);
}
// All served files with the latest timestamps
window.__karma__.files = {
'/base/node_modules/mocha/mocha.js': '253e2fdce43a4b2eed46eb25139b784adbb5c47f',
'/base/node_modules/karma-mocha/lib/adapter.js': '3664759c75e6f4e496fef20ad115ce8233a0f7b5',
'/base/test/custom-test.js': 'abf5b0b3f4dbb62653c816b264a251c7fc264fb9',
'/base/test/build/build.css': 'df7e943e50164a1fc4b66e0a0c46fc86efdef656',
'/base/test/build/build.js': '9f0a39709e073846c73481453cdee8d37e528856',
'/base/test/build/test.js': '0ccd4711b9c887458f81cf1dedc04c6ed59abe43'
};
</script>
<!-- Dynamically replaced with <script> tags -->
<script type="text/javascript" src="/base/node_modules/mocha/mocha.js?253e2fdce43a4b2eed46eb25139b784adbb5c47f"></script>
<script type="text/javascript" src="/base/node_modules/karma-mocha/lib/adapter.js?3664759c75e6f4e496fef20ad115ce8233a0f7b5"></script>
<script type="text/javascript" src="/base/test/custom-test.js?abf5b0b3f4dbb62653c816b264a251c7fc264fb9"></script></body>
Karma introduces the page scripts/html just like ajax, so it wont execute once the append has finished.
You will need to append the files for each spec. I have a helper for this job:
function appendCSS(path){
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href='base/' + path;
document.body.appendChild(link)
}
function appendScript(path){
var link = document.createElement('script');
link.type = 'javascript';
link.src='base/' + path;
document.body.appendChild(link)
}
function loadAssets(page){
document.body.innerHTML = __html__['_site/' + (page || 'index') + '.html'];
appendCSS('_site/styles/demo.css');
appendCSS('_site/styles/' + page + '.css');
appendScript('_site/scripts/vendor.js');
appendScript('_site/scripts/' + page + '.js');
}
module.exports = {
loadAssets: loadAssets
};
In my spec i then simply call the helper, passing the name of the html page to be tested.
require('../helper').loadAssets('tested-page-name');
As you can see, i use the borwserify plugin, but i hope this helps.

Flash calling a FBJS

i use FBML for the FB application
I have a flash and the flash suppose to call a javascript on the page. I read so many websites trying to figure it out but still having problem.
Here is the Webpage with the javascript:
<fb:fbjs_bridge/>
<div id="swfContainer"></div>
<script>
// the javascript to call and change the text of ztest001
function callmenow(a) {
var obj = document.getElementById("ztest001");
obj.setTextValue("Calling");
}
// generating the SWF
var swf = document.createElement('fb:swf');
swf.setId('my_swf_id');
swf.setWidth('630');
swf.setHeight('520');
swf.setSWFSrc('http://hollywood-life.madscience-games.com/ztest/test1.swf');
document.getElementById('swfContainer').appendChild(swf);
</script>
<div onclick="callmenow('a')">call me now</div>
<div id="ztest001"> YOo </div>
And here it is the Flash code. (one frame. code is on the frame. with a text box "myvar1")
var connection:LocalConnection = new LocalConnection();
var connectionName:String = LoaderInfo(this.root.loaderInfo).parameters.fb_local_connection;
connection.allowDomain("apps.facebook.com", "apps.*.facebook.com");
function callFBJS():void{
myvar1.text = "START"; // debugging purpose
if (connectionName) {
myvar1.text = "Connection now"; // debugging purpose
var pArray = ['bs'];
connection.send(connectionName, "callmenow", "callmenow", pArray);
myvar1.text = "SENT";
}
}
callFBJS();
Well, when I test, the flash loads. The code goes through everything and shows "SENT" in the flash text box. However, it doesn't seem like it is calling the javascript and changing the text in the HTML page.
Have I done something wrong? I tried the ExternalInteface.call method but this doesn't work either.
ALSO, when i run it in FireFox, not error popup.
however, when i run it in IE, I got this:
VerifyError: Error #1033: Cpool entry 36 is wrong type.
ReferenceError: Error #1065: Variable FBJS is not defined.
Did you try changing:
connection.send(connectionName, "callmenow", "callmenow", pArray);
to
connection.send(connectionName, "callFBJS", "callmenow", pArray);