Using Gamepads in Google Dart - google-chrome

I'm looking for code examples for the Gamepad API for Google Dart.
I've tried relying on the API doc directly and attempted to write out an instance for it.
//Gamepad Example:
Gamepad g = new Gamepad();
g.id.toString(); // This doesn't seem to return anything...
If anyone has any code examples for this that would be fantastic!

I tried this and it worked for me :
import "dart:html";
main(){
window.animationFrame.then(runAnimation);
}
runAnimation(timestamp){
var gamepads = window.navigator.getGamepads();
for (var pad in gamepads)
{
if(pad != null){
querySelector("#buttons").setInnerHtml("${pad.buttons.join(" ")}</br>${pad.axes.join(" ")}");
}
}
window.requestAnimationFrame(runAnimation);
}

I don't have a gamepad to try but
Gamepad g = new Gamepad();
doesn't work because Gamepade has no public constructor.
Try
var gp = window.navigator.getGamepads();
//or
// haven't tried because I don't know how to provoke this event without a gamepad
window.on['gamepadconnected'].listen((e) {
var gamepad = e.gamepad;
});
instead.
I get an empty list so I can't examine more.
see also:
- https://developer.mozilla.org/en-US/docs/Web/Guide/API/Gamepad
- https://dvcs.w3.org/hg/gamepad/raw-file/default/gamepad.html

Related

Create ko.observableArray from JSON object in Knockout JS

I have JSON object loaded to my view model. I want to push that into an observableArray.
function viewModel()
{
var self = this;
self.details = [{"id":1,"first_name":"fname1","last_name":"lname1","salary":1000.0},
{"id":2,"first_name":"fname2","last_name":"lname2","salary":2000.0},
{"id":3,"first_name":"fname3","last_name":"lname3","salary":3000.0}];
self.emp = ko.observableArray([new Model(self.details[0]),new Model(self.details[1]),new Model(self.details[2])]);
//This method works, but is very inefficient...
}
ko.applyBindings(new viewModel());
The solution I found was to feed each element individually, which is not practical.
I'm using JQuery. I found some solutions using knockout.mapping plugin. But I'm unable to add that plugin to my Eclipse workspace correctly.
I'm new to Knockout. Please help me find a solution to add the entire object to the observableArray.
I would probably use the mapping plugin personally, but this should also work
function viewModel()
{
var self = this;
self.details = [{"id":1,"first_name":"fname1","last_name":"lname1","salary":1000.0},
{"id":2,"first_name":"fname2","last_name":"lname2","salary":2000.0},
{"id":3,"first_name":"fname3","last_name":"lname3","salary":3000.0}];
var mapped = self.details.map(m => new Model(m));
//or
//var mapped = self.details.map(function(m) {return new Model(m);});
self.emp = ko.observableArray(mapped);
}
ko.applyBindings(new viewModel());

How to use setVolume() on SoundCloud

I want to use setVolume(0.01) on SoundCloud because it's way to loud for me.
As a temporary solution, I made the whole Chrome browser quieter, but then Youtube and other websites are too quiet.
I found that SoundCloud's volume can be controlled via the "SC.Widget method", but I have no idea what that is.
Can somebody explain how I can use that to set SoundCloud's volume to 0.01?
I already tried to put that just in the Chrome console but this gives me the following: VM1394:1 Uncaught ReferenceError: setVolume is not defined(…)
To change the volume on the Soundcloud website with a script there's no real API you can use, and no official way to do so. This should work:
webpackJsonp([], {
0: function(a, b, require) {
var modules = require.c;
modules[54].exports.broadcast("volume:set", 0.1);
}
});
Because the Soundcloude code is minified and not made to be used by other scripts, it is possible that the above solution might break with an error like this:
Uncaught TypeError: modules[54].exports.broadcast is not a function(…)
A hacky solution is to iterate over all modules and execute the volume:set broadcast:
webpackJsonp([], {
0: function(a, b, require) {
var modules = require.c;
for(var x in modules){
if(modules[x].exports.broadcast){
modules[x].exports.broadcast("volume:set", 1.0);
}
}
}
});
To change volume on Soundcloud widgets:
The HTML5 widget API is explained here. What you need to do first is include Soundcloud's API script, you can use this code which I borrowed from here:
var scapi = document.createElement('script');
scapi.src = "https://w.soundcloud.com/player/api.js";
document.getElementsByTagName('head')[0].appendChild(scapi);
The next step is finding the Soundcloud widget and use the API script to get the functionality we need, i.e. setVolume():
var sciframe = document.querySelector("iframe");
var widget = SC.Widget(sciframe);
widget.setVolume(0.1); // goes from 0 to 1
This you can use as a userscript with Greasemonkey or Tampermonkey and run it automatically:
var scapi = document.createElement('script');
scapi.src = "https://w.soundcloud.com/player/api.js";
document.getElementsByTagName('head')[0].appendChild(scapi);
function waitAndRegister() {
window.setTimeout(function(){
if(typeof(SC) == 'undefined') {
waitAndRegister();
} else {
quiet();
}
}, 100);
};
waitAndRegister();
function quiet() {
var scWidgets = document.querySelectorAll('iframe[src^="https://w.soundcloud.com/player"]');
if(scWidgets.length > 0) {
for (var i = 0; i < scWidgets.length; ++i) {
var widget = SC.Widget(scWidgets[i]);
widget.bind(SC.Widget.Events.PLAY, function() {
widget.setVolume(0.1);
});
}
}
}
Chrome > ctrl+shift+i > Application Tab > Storage > Local Storage > V2::local::settings
and set volume you need (eg: volume":0.05,")
Works like a charm for me.

Google Chrome Indexdb - redundant code

I am tryint yo understand some code from an opensource project that handles indexDB commands within a Google Chrome application.
The code is as follows :
var db = pm.indexedDB.db;
var trans = db.transaction([pm.indexedDB.TABLE_DRIVE_CHANGES], "readwrite");
var store = trans.objectStore(pm.indexedDB.TABLE_DRIVE_CHANGES);
var boundKeyRange = IDBKeyRange.only(driveChange.id);
var request = store.put(driveChange);
request.onsuccess = function (e) {
callback(driveChange);
};
request.onerror = function (e) {
console.log(e.value);
};
Although the app works, to me it seems that the following line is redundant code
var boundKeyRange = IDBKeyRange.only(driveChange.id);
Or am I missing something? The variable 'boundKeyRange' is never referenced anywhere.
Unless boundKeyRange is used later, you're not missing something. IDBKeyRange.only just creates an IDBKeyRange object, and if that object isn't used in some IndexedDB request, it does absolutely nothing.

Google Apps Script, HTML addClickHandler ServerHandler does NOT work

Can anyone confirm that HTML widgets accept ClickHandlers on the Server side ? I can't get my below code to work.
I create a serverHandler (and for good measure I have even added a useless callback element). Subsequently, I add it to a HTML.addClickHander (for good measure I have even added it to .addMouseUpHandler as well). The function is NOT executed.
var mouseclick = app.createServerHandler("handleTrainingClick_").addCallbackElement(lstFilter);
var params = [ "fromOrg", "trainingTitle", "dueDate", "medical", "status" ];
var resultSet = blSelectActiveTrainings_();
while (resultSet.hasNext()) {
var training = resultSet.next();
var html = TRAINING_ROW;
for (var pI in params) {
html = html.replace("$"+params[pI], training[params[pI]]);
}
pnlList.add(app.createHTML(html).setId(training.id).addClickHandler(mouseclick).addMouseUpHandler(mouseclick)
.addMouseMoveHandler(mousemove).addMouseOutHandler(mouseout).addMouseOverHandler(mouseover));
}
function handleTrainingClick_(e) {
Logger.log(e.source);
var app = UiApp.getActiveApplication();
return app;
}
HTML widgets server side handlers work just fine. It was an incorrect reference in my code. Thanks all.

Is there a work around for an object oriented front-end in Google Script

I'm having some problems with an event handler in the object below. I can't remember the error message but it basically said that it could not find the function. The code below is an example of what I'm trying to do.
var anObject = function () {
var n = 0;
var HandleClick(e) {
n ++;
};
return {
Init: function () {
var app = UiApp.getActiveApplication();
var handler = app.createServerHandler("HandleClick");
var com = UiApp.LoadComponent("MyGui", {prefix: "a"});
com.getElementById("button").addClickHandler(handler);
}
}
}
Would really appreciate a work-around if possible, if that is not possible then please tell me what you would suggest because I'm not sure how best to get around this.
Thanks guys.
All handler functions must be top level functions on your script. It's not possible to have it inside an object like this.