Chrome: Cannot read property 'getAll' of undefined - javascript

I'm new to Javascipt and just trying out some examples I found in the net. This one doesn't work at all. I open about:blank in Chrome and use a Console there. When I run this code there's an error Cannot read property 'getAll' of undefined'.
chrome.windows.getAll({populate:true}, getAllOpenWindows);
function getAllOpenWindows(winData) {
var tabs = [];
for (var i in winData) {
if (winData[i].focused === true) {
var winTabs = winData[i].tabs;
var totTabs = winTabs.length;
for (var j=0; j<totTabs;j++) {
tabs.push(winTabs[j].url);
}
}
}
console.log(tabs);
}
Also it seems like chrome doesn't have property windows in it...

That seems like it is using the Chrome Extensions API. You can't just run it in the console. You can get started with Chrome extension development here.

Related

Localstorage does not work on local webpage: security error

When using my webpage (http://localhost/mypage.html) accessing localStorage issues a security error:
Uncaught DOMException: Failed to read the 'localStorage' property from 'Window': Access is denied for this document.
It is just a simple line
res = localStorage.getItem(name);
and even
console.log(localStorage);
issues the same error.
But it is a LOCAL site, so no cross domains are used.
What goes wrong here ?
This problem is related to a Chromium bug which is now fixed. See https://community.brave.com/t/html5-localstorage/100843
You can check if your current version is affected with this JSFiddle : https://jsfiddle.net/6sm54/2/
function lsTest(){
var test = 'test';
try {
localStorage.setItem(test, test);
localStorage.removeItem(test);
return true;
} catch(e) {
return false;
}
}

Disabling Chrome Console Developers Tools

I tried disabling the console but it didnt work in my chrome for some reason.
How does Facebook disable the browser's integrated Developer Tools?
Code
$(document).ready(function(){
with ((console && console._commandLineAPI) || {}) {
(function() {
try {
var $_console$$ = console;
Object.defineProperty(window, "console", {
get: function() {
if ($_console$$._commandLineAPI)
throw "Sorry, for security reasons, the script console is deactivated on netflix.com";
return $_console$$
},
set: function($val$$) {
$_console$$ = $val$$
}
})
} catch ($ignore$$) {
}
})();
}
});
function abs(){
return "Hey Its not Working";
}
When i run abs() from console it shows me output Hey Its not Working which means its not disabled. So am i doing something wrong ?
Google repaired the console so that it is no longer possible to disable it in that way.
Note that Facebook does still add a warning message to the console, but you can still freely run arbitrary code there.
At this time there is no mechanism to prevent someone from using the Developer Tools to... well, develop stuff.

Javascript Console.log Not working in Chrome, or Firefox

I am doing a very simple:
console.log("Testing");
along with :
alert("testing");
The Alert works (so I know the javascript is working) but I'm unable to see the log. When I use Firefox I get the following error:
The Web Console logging API (console.log, console.info,
console.warn, console.error) has been disabled by a script on this
page.
What is going on? I have looked at the following topics, but none have helped:
Chrome: console.log, console.debug are not working
console.log quit working in Chrome
Console.log not working in Chrome [closed]
why does console.log not output in chrome?
Console.log not working at all
I have also made sure that the funnel is working and that logging is turned on.
What else could the problem be?
I just came across this problem after a Firefox update and managed to fix it. Here's the code that caused the problem:
/* IE fix that allows me to still log elsewhere */
if (typeof(console)=="undefined") {
var console = {
output: null,
log: function (str) {
// we can't emulate the console in IE, but we can cache what's output
// so that IE users can get it via console.output
if (!this.output) this.output = new Array();
this.output.push(new String(str));
}
};
window.console = console;
}
In the previous version of FireFox, "var console;" wouldn't get executed. Now it seems to have added some sort of branching/prediction mechanism. Seeing that I may define a variable called console with global scope, it disables window.console.
I fixed this issue by renaming var console; to var cons;
/* IE fix that allows me to still log elsewhere */
if (typeof(console)=="undefined") {
var cons = {
output: null,
log: function (str) {
// we can't emulate the console in IE, but we can cache what's output
// so that IE users can get it via console.output
if (!this.output) this.output = new Array();
this.output.push(new String(str));
}
};
window.console = cons;
}
I still need to test this to make sure it does what I expect in IE, though. I just need to find a copy of IE without a console (I think 9 or below).
I just wish that Firefox would tell you what line of what script disabled the console - that would be nice.
I had the same issue with a Magento e-commerce site (version 1.6.2.0).
I fixed it commenting the following lines in /js/varien/js.js:637
if (!("console" in window) || !("firebug" in console))
{
var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
"group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
window.console = {};
for (var i = 0; i < names.length; ++i)
window.console[names[i]] = function() {}
}
This fix is only (obviously) for Magento sites.
Turns out the theme developer had added firebug lite to the theme without me knowing. Turning it off fixed the problem.
I think the page you are viewing when trying to log via console.log() has a script in it which overwrites the property window.console.log. Usually this property is preset with a function by the browser but you a script may override it.
First suggestion: Do you use any other dev tools that use console? On Firefox I had the same problem with Firebug running on background without me noticing it, after closing firebug the error went away. Here's a possiple duplicate thread: Firefox Web Console Disabled?
Second, if it is overridden by some script, then one by one disable scripts and see which draws the error.

Custom error object not correctly rendered in Chrome

I have created a custom Error object:
function DialogException(message, data) {
this.name = "DialogException";
this.message = message;
this.data = data;
}
DialogException.prototype = new Error();
DialogException.prototype.constructor = DialogException;
This is the reccomended way of doing it (right?):
[MDN JavaScript Refference]
When throwing this Error:
throw new DialogException('Missing Dialog Settings (dialogDescriptor.dialog).');
Firefox console is showing it this way:
Google Chrome Console is showing it that way:
Can someone please explain why Chrome does not render the custom error object correctly? I would expect an output like the one from Firefox.
Chrome Version: 26.0.1410.64 m
Firefox Version: 19.0.2

why does document.all.item does not work on localhost?

I am working on a very old web app which uses document.all.item to iterate through the dom and
it is causing runtime errors when i deploy the application in localhost and the error dissapears when i deploy it on server outside my machine. Below os the code where it throws the unknown runtime error. what is the reason or how can i go about resolving it?
with(document.all)
item('fieldName').innerHTML = "Blah Blah"; // Error is on this line.?
}
When i tried to debug in IE. I can access the item but cannot access the innerHTML for some reason. Is it because of IE or something else?
On Google Chrome, HTMLAllCollection (The result from document.all) has no item property. I'd suggest looping through them with an for loop:
var items = document.all;
var length = items.length;
for(var i = 0; i < length; i++){
//Do something with: items[i];
}
The behaviour change might be because of inconsistent implementation of a item property on HTMLAllCollections, where IE apparently does implement it, and your server doesn't.

Categories

Resources