JSHint and "Tolerate Variable Shadowing" - jshint

Does anybody know why this code does not produce errors in JSHint?
I think it should give me a variable shadowing warning but I'm not getting one.
I have "Tolerate Variable shadowing" as false am using the visual studio plugin.
RES.test = function () {
var test, f;
f = function () {
var test;
window.alert(test);
};
};
Thanks.

I just stumbled onto this too. Apparently JSHint developers' definition of "shadowing" is not what you expect. For them, hiding a variable name coming from a closure is not shadowing. And yes, I find it strange too :-)
If you look at their test suite, they mean something like "redefinition", where you do
var a = 1;
...
var a = 2;
look at their test case: https://github.com/jshint/jshint/blob/master/tests/stable/unit/fixtures/redef.js

Related

Method/property does not exist

I'm trying to convert the JavaScript code
if (window.ifEdit.editIsDirty()) { }
into Typescript. I got as far as the following
var iframe = document.getElementById('ifEdit');
var iWindow = <HTMLIFrameElement>(iframe).contentWindow;
var _editIsDirty = iWindow.editIsDirty();
I get the red squiggles under 'contentWindow' and 'editIsDirty' saying the method/property does not exist on the type. The .ts doesn't compile to a .js file.
I have searched, but did not manage to find a solution.
For the contentWindow part, the problem with your code is that the casting is done wrong, should be:
var iWindow = (<HTMLIFrameElement> iframe).contentWindow;
As for the editIsDirty, it's not a standard property of Window.
If it's something which is added in the environment in which you are running your javascript then you need to declare it like so:
interface IfEdit {
editIsDirty(): boolean;
}
interface Window {
ifEdit: IfEdit;
}
var iframe = document.getElementById("ifEdit");
var iWindow = (<HTMLIFrameElement> iframe).contentWindow;
var _editIsDirty = iWindow.ifEdit.editIsDirty();
Use the code in Playground.
Casting will be through as. this assures .contentWindow is accessible.
const iframe = document.getElementById('embed-player') as HTMLIFrameElement;
if (!iframe) {
// Handle case where iframe not found
return;
}
const contentWindow = iframe.contentWindow;
// Note: You will likely need more null handling for contentWindow's properties
console.log(contentWindow?.document);

How to stop warnings for functions without DefinitelyTyped?

If some function or library does not have DefinitelyTyped, I know these two ways to stop warnings.
interface Navigator {
getUserMedia: any
}
declare let RTCIceCandidate: any;
But right now, this 3rd-part library Collection2 is used like this:
let ProductSchema = {};
let Products = new Mongo.Collection('products');
Products.attachSchema(ProductSchema);
It give me a warning:
Property 'attachSchema' does not exist on type 'Collection'.
I tried the way below, but it does not work.
interface Collection {
attachSchema: any
}
How can I stop this warning? Thanks
EDIT:
Eric's adding any way solves the problem.
let Products:any = new Mongo.Collection('products');
Products.attachSchema(ProductSchema);
But now a new trouble comes:
let UserSchema = {};
Meteor.users.attachSchema(UserSchema);
Since Meteor.users is built in, so there is no place to add any. How to solve this? Thanks
Thanks for Amid's help. So the way is:
(<any>Meteor.users).attachSchema(UserSchema);

Trying to create VB.Net Integration for EdgeJS

I have created this project, which is basically an attempted clone of this project but converted from C# to VB using SharpDevelop 4.4 and then built using VS 2015
My issue can be found on GitHub here, but here's the error I'm getting when I run my NodeJS project:
My bit of code in my NodeJS project that isn't working:
var WriteCrapVB = edge.func('vb', function () {
/*
Function(input)
Console.WriteLine("Hello from .NET")
Return Nothing
End Function
*/
});
var hello = WriteCrapVB(null);
hello(null); // prints out "Hello from .NET"
When running this C# it does work:
var WriteCrapCS = edge.func('cs', function () {
/*
async (input) =>
{
return (Func<object,Task<object>>)(async (i) => {
Console.WriteLine("Hello from .NET");
return null;
});
}
*/
});
var hello = WriteCrapCS(null, true);
hello(null, true); // prints out "Hello from .NET"
I have basically tried to use this guide to create this project.
I've tried various things to fix this bug, each as unhelpful as the previous one. I'm hoping someone with greater .NET knowledge than I can point out a glaringly obvious mistake!
Please help this poor soul from going bald from hair tearing!
facepalm
Root namespace must be blank.

Handlebars.js: how does partials gets invoked in a helper? I got: TypeError: fn is not a function

I am learning Handlebars. It still appears to be a mystery to me that how a partials gets invoked in a helper.
I read this tutorial: http://blog.teamtreehouse.com/handlebars-js-part-2-partials-and-helpers
From this example in the tutorial,
Handlebars.registerHelper("stripes", function(array, even, odd, fn) {
var buffer = "";
for (var i = 0, j = array.length; i < j; i++) {
var item = array[i];
// we'll just put the appropriate stripe class name onto the item for now
item.stripeClass = (i % 2 == 0 ? even : odd);
// show the inside of the block
buffer += fn(item); <!-- this is where a partials gets invoked -->
}
// return the finished buffer
return buffer;
});
it appears the partial is added and applied by Handlebars. However, I used this same approach in Handablebars 1.3.0 and 2.0 Alpha-2, it seems no longer the case. I always got the error:
TypeError: fn is not a function
buffer += fn(item);
I did quite online search and found a number tutorials, but none of them shows how partials is hooked up with a helper in version 1.3.0 or later.
Can someone help me out?
Thanks a lot!
OK. I believed I solved this problem. In v1.0 or later, Handlebars puts everything in a hash, not just fn. So, everything in the above post still is valid except these two lines:
Handlebars.registerHelper("stripes", function(array, even, odd, options)
...
buffer += options.fn(item);
Hope this helps someone else. Any confirmation is welcome. I feel bad that there is no direct example on this, at least I did not find it.
Regards.

"eval is not a function" when using iframe and Greasemonkey/Scriptish

Is there any way I can get this piece of code to work inside Greasemonkey/Scriptish, or would I have to inject it into the webpage itself?
body = document.getElementsByTagName("body")[0];
fakeConsole = 'window.top._console';
injected = document.getElementById("sandbox") ? true : false;
sandboxframe = injected ? document.getElementsById("sandbox") : document.createElement('iframe');
sandbox = null;
if (!injected) {
body.appendChild(sandboxframe);
sandboxframe.setAttribute('id', 'sandbox');
sandboxframe.setAttribute('style', "display:none")
}
var p = sandboxframe.contentWindow.eval('1 + 1');
console.log(p);
This code does work when using source:
<script type="text/javascript" src="test.js"></script>
But not when using in a Greasemonkey script, I have observed there's some kind of security barrier I'm not quite familiar with and attempted to use unsafeWindow to bypass XPCNativeWrapper.
Please shed some light on this.
Several things:
The code has an error; getElementsById is not a function.
That code otherwise does work in Greasemonkey 1.0 or later, if the #grant none directive applies. More on this below.
For Scriptish, all other browsers, and Greasemonkey scenarios where #grant none is not possible; you will have to "inject" the code. More on this below.
As Jeremy J Starcher says, eval() should be avoided as much as possible. eval() makes performance, maintenance, debugging, and security much harder.
For Greasemonkey 1.0, and later:
In some scenarios, Greasemonkey no longer uses the XPCNativeWrapper. See the doc for the #grant directive.
So this means that (1) If your script uses no GM_ functions and (2) the script specifies #grant none, then your code will run as-is (excepting the getElementsById typo).
Note that no other scripting engine does this. (For darn good reasons. Greasemonkey's new behavior concerning #grant, and the sandbox, is controversial at best.)
If you wish to use GM_ functions, then you must inject the iframe code. See the next section.
For Scriptish, Privileged Greasemonkey, Chrome, etc.:
Scriptish, Sandboxed Greasemonkey, Chrome, etc. all do not handle iframes well from within their respective sandboxes. (See these Q's, for example.)
The only reliable way to run this kind of code from a GM/userscript is to inject it. Like so:
function gmMain () {
body = document.getElementsByTagName("body")[0];
fakeConsole = 'window.top._console';
injected = document.getElementById("sandbox") ? true : false;
sandboxframe = injected ? document.getElementById("sandbox") : document.createElement('iframe');
sandbox = null;
if (!injected) {
body.appendChild(sandboxframe);
sandboxframe.setAttribute('id', 'sandbox');
sandboxframe.setAttribute('style', "display:none")
}
var p = sandboxframe.contentWindow.eval('1 + 1');
console.log(p);
}
addJS_Node (null, null, gmMain);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
var D = document;
var scriptNode = D.createElement ('script');
if (runOnLoad) {
scriptNode.addEventListener ("load", runOnLoad, false);
}
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}

Resources