HIdDevice.fromIdAsync always returns null - javascript

I spent way too much time trying to understand the problem here. I am working with a HID Barcode Scanner, and am able to get the device information. But I am unable to get a hold of the HidDevice object even with the right device id. It always return null. Here is what I have:
var selector = Windows.Devices.HumanInterfaceDevice.HidDevice.getDeviceSelector(parseInt('0x1', 16), parseInt('0x6', 16));
Windows.Devices.Enumeration.DeviceInformation.findAllAsync(selector, null).then(
function (deviceInfoCollection) {
if (deviceInfoCollection.length > 0) {
for (var i = 0; i < deviceInfoCollection.length; i++) {
var id = deviceInfoCollection.getAt(i).id;
return Windows.Devices.HumanInterfaceDevice.HidDevice.fromIdAsync(id, Windows.Storage.FileAccessMode.readWrite);
}
}
else {
throw "No Devices Discovered.";
}
})
.done(function (device) {
if (device != null)
successCallback(device.name);
});
I added these device capabilities in my manifest file:
<DeviceCapability Name="humaninterfacedevice">
<Device Id="any">
<Function Type="usage:0001 *"/>
</Device>
</DeviceCapability>

I'm going through the same issue now. The only thing I see in your code that strikes me as odd is the following manifest tag:
<Device Id="any">
Usually, the "any" value works. But I've had issues arise where the vendor and product id are required; I'm not quite sure why, but I think it's based off the type of device/usageid. I would try hardcoding the vendor and product id's to see if it makes a difference.
Another thought: I'm guessing by the usage tag that your scanner is configured as a keyboard. You can check to see if your scanner can be configured as a non-keyboard HID device, which helped me personally. I see other people on the internet having issues where an HidDevice is returned as null because another program is using that device; in your case, the OS might already be using the keyboard and locking it out somehow.
Best of luck!

Related

"Permission Denied" error in IE doesn't seem to match the javascript code

Running in IE is a legacy app built with frames that makes alot of references cross-frame like parent.header.blah.blah and parent.sidebar.so.and.so. Worked fine in old IE compatibility mode. Works in chrome and edge (chromium).
But in regular IE without compatibility mode on, it's throwing a permission denied error on line 43. Thing is, it throws the error in the console NO MATTER WHAT IS ON LINE 43!!! I added superfluous lines of code to push other code down, took out code to move other code up. Doesn't matter, the console ALWAYS says it's on line 43.
I put breakpoints in and noticed the error doesn't actually add to the console until AFTER the javascript has finished running. The page is very large with ALOT of javascript, and it's dificult to comment a section out without breaking the page to experiment with what might be causing the permission denied.
Permission Denied is supposed to indicate a same-origin violation as I understand it, but all frames and files are coming through the same servlet on the same URL with only parameters changing. I printed out the document.domain of every frame, they all are identical.
So..I'm not even sure what to do at this point to narrow it down. How can I figure out what is really the offending piece of code...or even section?
UPDATE - So it seems that the error is actually coming from a function in another frame being called from this frame (nowhere near line 43 by the way). That function is managing the options in a select list. The actual error comes here:
for (var k=0; k < assetListz.options.length; k++) {
if (assetListz.options[k].value == currentAsset) { //permission denied!
inList = true;
assetListz.options[k].selected = true;
break;
}
}
assetListz didn't have a 'z' on it until I just did that to make sure I wasn't accidentally getting scope to some OTHER assetList. I can test the length of the assetList, but as soon as I check the value on that second line, kaboom. Ideas?
Update 2 -
I changed the code to get the assetlist in each reference. No storing it. Blows up in the same place.
for (var k=0; k < document.getElementById('assetList').options.length; k++) {
if (document.getElementById('assetList').options[k].value == currentAsset) {
inList = true;
document.getElementById('assetList').options[k].selected = true;
break;
}
}
Okay, I got this fixed. I'm not sure exactly WHY this fix works, though I can guess. It seems that by rewriting the method so that it avoids use of the options array on the select object, everything works fine. I reference it just once to get the length...which it allows. but if i try to get a specific option by assetList.options[i].anything, then I get permission denied.
I still think this is a bug in IE11's same-origin code, but lucky for me, it seems like MS didn't re-use code so the same bug didn't 'protect' all means of accessing the select options. Just via the array property. Or maybe something else is goin on. I just know this worked for me.
//By changing the value attribute, we change the current selection.
assetList.value = currentAsset.toUpperCase();
if(assetList.selectedIndex == -1) {
//this means the current asset wasn't in the list.
if(assetList.options.length >= 1000) {
//not allowed to add more than 1000. And if so, set the selectedIndex back
alert("The Active Asset List contains the maximum of 1000 entries. \n" +
"The current Asset ID '" + currentAsset + "' was not added to the Active Asset List.");
assetList.selectedIndex = currentSelectedIndex;
return;
} else {
var option = document.createElement("OPTION");
option.value = currentAsset;
option.text = currentAsset;
assetList.add(option, 0);
assetList.selectedIndex = 0;
}
}

Cannot get ExecuteScriptAsync() to work as expected

I'm trying to set a HTML input to read-only using ExecuteScriptAsync. I can make it work, but it's not an ideal scenario, so I'm wondering if anyone knows why it doesn't work the way I would expect it to.
I'm using Cef3, version 63.
I tried to see if it's a timing issue and doesn't appear to be.
I tried invalidating the view of the browser but that doesn't seem to help.
The code I currently have, which works:
public void SetReadOnly()
{
var script = #"
(function(){
var labelTags = document.getElementsByTagName('label');
var searchingText = 'Notification Initiator';
var found;
for (var i=0; i<labelTags.length; i++)
{
if(labelTags[i].textContent == searchingText)
{
found = labelTags[i]
break;
}
}
if(found)
{
found.innerHTML='Notification Initiator (Automatic)';
var input;
input = found.nextElementSibling;
if(input)
{
input.setAttribute('readonly', 'readonly');
}
}})()
";
_viewer.Browser.ExecuteScriptAsync(script);
_viewer.Browser.ExecuteScriptAsync(script);
}
now, if I remove
found.innerHTML='Notification Initiator (Automatic)';
the input is no longer shown as read-only. The HTML source of the loaded webpage does show it as read-only, but it seems like the frame doesn't get re-rendered once that property is set.
Another issue is that I'm executing the script twice. If I run it only once I don't get the desired result. I'm thinking this could be a problem with V8 Context that is required for the script to run. Apparently running the script will create the context, so that could be the reason why running it twice works.
I have been trying to figure this out for hours, haven't found anything that would explain this weird behaviour. Does anyone have a clue?
Thanks!

jQuery.when.done() seems to not work in IE9. No console error either

I have written a jquery addon, with a little help from the internet, which retrieves data from Facebook and does as intended on all browsers tested so far apart from IE9.
I work for local government and unfortunately we still use IE9 in our builds (It was still IE8 a few weeks back!! So could have been a lot worse I expect :).
Anyways, I digress, I have added the section of code below which never completes in IE9, but does in IE10, and other browsers...
Can anyone explain/help me adapt or fix this snippet so that I can get it working in IE9?? And not break it in any other browsers in the process :)??
$.when($.getJSON(ogUSER), $.getJSON(ogPOSTS)).done(function (user, posts) {
// user[0] contains information about the user (name and picture);
// posts[0].data is an array with wall posts;
var fb = {
user: user[0],
posts: []
};
var idxLimit = 0;
$.each(posts[0].data, function () {
// We only show links and statuses from the posts feed:
if (this.type != 'link' && this.type != 'status') {
return true;
}
// Copying the user avatar to each post, so it is
// easier to generate the templates:
this.from.picture = fb.user.picture.data.url;
// Converting the created_time (a UNIX timestamp) to
// a relative time offset (e.g. 5 minutes ago):
this.created_time = relativeTime(this.created_time * 1000);
// Converting URL strings to actual hyperlinks:
this.message = urlHyperlinks(this.message);
//remove all anchors
//var content = $('<div>' + this.message + '</div>');
//content.find('a').remove();
//this.message = content.html();
fb.posts.push(this);
idxLimit++;
if (idxLimit === 2) {
return false;
}
});
In all browsers, not including IE9, if I insert a breakpoints anywhere within the .done() callback it stops execution and I can debug. With IE9 the breakpoint is not reached leading me to believe there is an issue with IE9 script engine and jQuery.when() API call, or the .done() callback method...
But, I'm just guessing at the mo... I've been searching the web for the last few hours to see if anyone else has happened upon a similar issue but to no avail. I hope some of the more experienced coders here can help... would be very much appreciated. Until then the search goes on :)
Thanks for your time folks ;)
PS. I don't receive any console errors what so ever in IE9 running the script...
TartanBono

Error handling "Out of Memory Exception" in browser?

I am debugging a javascript/html5 web app that uses a lot of memory. Occasionally I get an error message in the console window saying
"uncaught exception: out of memory".
Is there a way for me to gracefully handle this error inside the app?
Ultimately I need to re-write parts of this to prevent this from happening in the first place.
You should calclulate size of your localStorage,
window.localStorage is full
as a solution is to try to add something
var localStorageSpace = function(){
var allStrings = '';
for(var key in window.localStorage){
if(window.localStorage.hasOwnProperty(key)){
allStrings += window.localStorage[key];
}
}
return allStrings ? 3 + ((allStrings.length*16)/(8*1024)) + ' KB' : 'Empty (0 KB)';
};
var storageIsFull = function () {
var size = localStorageSpace(); // old size
// try to add data
var er;
try {
window.localStorage.setItem("test-size", "1");
} catch(er) {}
// check if data added
var isFull = (size === localStorageSpace());
window.localStorage.removeItem("test-size");
return isFull;
}
I also got the same error message recently when working on a project having lots of JS and sending Json, but the solution which I found was to update input type="submit" attribute to input type="button". I know there are limitations of using input type="button"..> and the solution looks weird, but if your application has ajax with JS,Json data, you can give it a try. Thanks.
Faced the same problem in Firefox then later I came to know I was trying to reload a HTML page even before setting up some data into local-storage inside if loop. So you need to take care of that one and also check somewhere ID is repeating or not.
But same thing was working great in Chrome. Maybe Chrome is more Intelligent.

Javascript onclick not called on some computers

I have an VB.NET web page that calls a javascript function when a checkbox in a datagrid view is checked or unchecked. The code works fine on my computer and for a few more people. But for most of the people the javascript function is not called at all when they check or uncheck a checkbox. All of these computers are Windows 7 and using IE 9. I also checked the IE->Internet Options->Advanced tab and the settings are the same on these machines. At this point I am out of ideas on this and google doesn't return any helpful results. I would really appreciate if someone can help me resolve this issue. Here is the code for the javascript function if that helps.
function SelectLineItem(pRowIndex)
{
var vGridView=document.getElementById('dgvFSOView');
var vLen=vGridView.rows.length;
var i=parseInt(pRowIndex)+1;
var intcount;
var vtxtFcheckbox=vGridView.rows[parseInt(pRowIndex)].cells[0].getElementsByTagName("input")[0].id;
var vFCheckbox=document.getElementById(vtxtFcheckbox);
var browser=navigator.appName;
for(intcount=i;intcount<=vLen-1;intcount++)
{
if ((document.getElementById('hifCheck').value=="ALL") || (document.getElementById('hifCheck').value=="-1"))
{
var vtxtSONO=vGridView.rows[intcount].cells[3].innerText;
}
else if((document.getElementById('hifCheck').value!="ALL") || (document.getElementById('hifCheck').value=="-1"))
{
var vtxtSONO=vGridView.rows[intcount].cells[2].innerText;
}
if(vtxtSONO=="")
{
if ((document.getElementById('hifCheck').value=="ALL") || (document.getElementById('hifCheck').value=="-1"))
{
var vtxttocheckbox=vGridView.rows[intcount].cells[14].getElementsByTagName("input")[0].id;
}
else
{
var vtxttocheckbox=vGridView.rows[intcount].cells[13].getElementsByTagName("input")[0].id;
}
var vTCheckbox=document.getElementById(vtxttocheckbox);
if(vFCheckbox.checked==true)
{
vTCheckbox.checked=true;
}
else
{
vTCheckbox.checked=false;
}
}
else
{
return false;
}
}
}
This could potentially be an issues with the permissions the users have on their computers. Security settings have different levels of permissions which vary depending on how the user set up their machine.
If you are running the code locally (IE: double clicking a local .html file) you often need to enable Javascript before any javascript can run at all on the page. If it's hosted on a website (IE: www.example.com/mypage.asp) then this shouldn't be a problem.
How are you calling this function? You may also want to try using the developer tools built into IE to see if any errors are occurring, which would prevent your script from completing.
Utilizing a cross browser compatible framework like jQuery might help your checkbox change event trigger, but could be overkill depending on what how complicated your project is.
Checkboxes do not reliably trigger the click event. Bind your function to the checkboxes' change event instead.

Categories

Resources