I have a firefox extension, and I would like to know how can I see witch file my script is running on.
I'm using window.location.href but in my case is not so useful.
Because I want to know what kind of file, for example, i just want my script be run on html files.
How can I do this? some ideas?
You can throw an Error, catch it and inspect the callstack.
var frames = [];
try {
throw new Error("debug");
} catch (exception) {
if (exception.stack && typeof exception.stack === "string") {
var lines = exception.stack.split("\n");
for (var i = 0; i < lines.length; i += 1) {
var frame = lines[i].match(new RegExp("^(.*)#(.*):(.*)$"));
frames.push({
"function": frame[1] || "anonymous",
"line": frame[3],
"file": frame[2]
});
}
}
}
console.log(frames);
fixed:
window.addEventListener('load', function () {
if (document.doctype) {
if (document.doctype.name == 'html') {
easy :)
thank you anyway for your help
Related
timeseries is an array in the following format:
[ {"date": "2012-12-21", "price": 1.234}, ... ]
My code:
function first(timeseries) {
if (timeseries.length === 0) {
return undefined;
}
var earliestIndex = 0;
for (var i = 0; i < timeseries.length; i++) {
if (timeseries[i].date === null) {
throw new Error("no date");
} else {
if(Date.parse(timeseries[i].date) < Date.parse(timeseries[earliestIndex].date)) {
earliestIndex = i;
}
}
}
return timeseries[earliestIndex].price;
}
Test result:
The question did not specify the exact value of date when not provided.
Why is this so? I have already thrown an error.
Couple of suggestions to cover all cases.
Test if timeseries[i].price is null or not before returning.
You may want to try using == instead of === as it will also do the necessary type conversions before check.
Reference: https://stackoverflow.com/a/359509/4874271
Tip: COPY the code and format it here instead of posting photos, would be easier for people to answer. :)
I have a design document written in javascript (someone else wrote this function) for a Cloudant database. This function is created to update a document. Within this document I want to first make a call to JSON.minify which I have found some code for online at https://www.npmjs.com/package/jsonminify
The code for the update function is below.. and I want to know how to make a call to JSON.minify from the code as suggested within the link provided: JSON.parse(JSON.minify(str));
Where I currently have _ref = JSON.parse(reqBody) I want to use _ref = JSON.prase(JSON.minify(reqBody));
Can someone tell me how I can call this external code from a design doc in Cloudant. (Cloudant works very similar to CouchDB in most cases, so I think it may be the same answer)
Thanks in advance!
function(doc, req) {
if (!doc) {
return [doc, JSON.stringify({ status: 'failed' })];
}
var reqBody=req.body;
_ref = JSON.parse(reqBody);
for (k in _ref) {
v = _ref[k];
if (k[0] === '/'){
nestedDoc = doc;
nestedKeys = k.split('/');
_ref1 = nestedKeys.slice(1, -1);
for (_i = 0, _len = _ref1.length; _i < _len; _i++){
nestedKey = _ref1[_i];
nestedDoc = ((_ref2 = nestedDoc[nestedKey]) != null ? _ref2 : nestedDoc[nestedKey] = {});
}
k = nestedKeys.slice(-1)[0];
if (v === '__delete__'){
delete nestedDoc[k];
}
continue;
}
if (v === '__delete__'){ delete doc[k]; }
else{ doc[k] = v; } }
return [ doc, JSON.stringify({ status: 'success' }) ];
}
You should be able to either include the source code at the top of your update function, or load it as a CommonJS module.
Have you tried either one?
Hi I am planning to find menu item by traversing recursively all the sub menus .
I have written script but I could not able to debug this console script. I am new to Firebug. Can some one enlighten me on either how to debug this script or a fix to the written script is also welcome.Thanks in advance.
var QuickActionFinder = function(component, searchString)
{
console.log(component.id);
if(component.hasOwnProperty('text'))
{
if(component.text == searchString)
{
if(component.hasOwnProperty('textEl'))
{
component.textEl.dom.click();
return;
}
}
}
if(component.hasOwnProperty('menu'))
{
var count = component.menu.items.length;
for(var i=0;i < count;count++)
{
var comp = component.menu.items.itemAt(i);
QuickActionFinder(comp,searchString)
}
}
else
{
return;
}
}
var comp = window.frames[2].Ext.getCmp('ext-comp-2515'); QuickActionFinder(comp,'Mobile')
I am trying to parse HTML element by class on Google Sites, my code is:
function doGet(){
var html = UrlFetchApp.fetch ('http://indicadoresdeldia.cl/').getContentText();
var doc = XmlService.parse(html);
var html = doc.getRootElement();
var menu = getElementsByClassName(html, 'span3 utm')[0];
var output = XmlService.getRawFormat().format(menu);
return HtmlService.createHtmlOutput(output);
}
Ween i run the code appear the nexte error message ReferenceError: "getElementsByClassName" is not defined.
i am trying to deploy the example for the next page: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html
Any ideas?
THanks in advance for your help.
According to that site, you should directly copy those functions to your project (source code available there) and then call them. That would alleviate each and every one of your problems.
Source: https://sites.google.com/site/scriptsexamples/learn-by-example/parsing-html
function getElementsByClassName(element, classToFind) {
var data = [];
var descendants = element.getDescendants();
descendants.push(element);
for(i in descendants) {
var elt = descendants[i].asElement();
if(elt != null) {
var classes = elt.getAttribute('class');
if(classes != null) {
classes = classes.getValue();
if(classes == classToFind) data.push(elt);
else {
classes = classes.split(' ');
for(j in classes) {
if(classes[j] == classToFind) {
data.push(elt);
break;
}
}
}
}
}
}
return data;
}
I was trying to show a text gradually on the screen (like marquee). e.g. H.. He.. Hell.. Hello. when I'm tracing it in debug in VS2010 it's working! but when it's actually running it shows the whole sentence at once.
I made a certain "delay" for about 3 seconds between each letter so it would suppose to take a while, but in reality it's shows everything immediately.
Who's the genius to solve this mystery? (please don't give me advices how to create the marquee effect, it's not the issue anymore. now it's just a WAR between me and javascript!) I'm assuming that it has to do with synchronization when calling function from function?
Thanks to whomever will help me get my sanity back.
you can download the code from here (VS project):
http://pcgroup.co.il/downloads/misc/function_from_function.zip
or view it here:
<body>
<script type="text/javascript">
//trying to display this source sentence letter by letter:
var source = "hi javascript why are you being such a pain";
var target = "";
var pos = 0;
var mayGoOn = false;
//this function calls another function which suppose to "build" the sentence increasing index using the global var pos (it's even working when following it in debug)
function textticker() {
if (pos < source.length) {
flash();
if (mayGoOn == true) {
pos++;
mayGoOn = false;
document.write(target);
textticker();
}
}
}
function flash() {
//I tried to put returns everywhere assuming that this may solve it probably one of them in not necessary but it doesn't solve it
if (mayGoOn == true) { return; }
while (true) {
var d = new Date();
if (d.getSeconds() % 3 == 0) {
//alert('this suppose to happen only in about every 3 seconds');
target = source.substring(0, pos);
mayGoOn = true;
return;
}
}
}
textticker();
</script>
You're obviously doing it wrong. Take a look at this.
var message = "Hello World!";
function print(msg, idx) {
if(!idx) {
idx = 0;
}
$('#hello').html(msg.substring(0, idx));
if(idx < msg.length) {
setTimeout(function() { print(msg, idx + 1) }, 200);
}
}
print(message);
Demo: http://jsbin.com/evehus