Firefox-SDK contentScript returns null - javascript

I am currently writing a Firefox-Addon that automatically prints every new file from my schools website. All the files on the website are in a table and every row (where each file is) has the class "s2d". This is what i have so far:
var self = require("sdk/self");
var uploads;
pageWorker = require("sdk/page-worker").Page({
contentURL: "http://uchronski.de/lernmaterialien/beruflicheschulformen/11fosinformationssystemeundnetzwerktechnik/index.php",
contentScript: ['var uploads = document.getElementsByClassName("s2d");', 'self.port.emit("pageEntriesLenght", uploads.lenght);']
});
pageWorker.port.on("pageEntriesLenght", function(files) {
uploads = files;
});
Here i'm trying to get the current amount of files hosted on the website but "files" is alway null. I have tried it with a pageMod, which worked, but I need to do it with a page worker instead and I have no idea what I did wrong.

You made a spelling mistake in the word 'length' in the contentScript. Try this:
contentScript: ['var uploads = document.getElementsByClassName("s2d");', 'self.port.emit("pageEntriesLenght", uploads.length);']

Related

App Script Tool: Trying to get all files URLs in the folder

I`m trying to get output of all the files URLs form the current folder, but I only get URL of the first file from the forlder here:
function listFolderContents() {
var sheet = SpreadsheetApp.getActive().getSheetByName('Hub 2');
var foldername = sheet.getRange('D160').getValue();
var linkoutput = sheet.getRange('W160');
var folders = DriveApp.getFoldersByName(foldername);
var folder = folders.next();
var contents = folder.getFiles();
Logger.log(linkoutput);
var file;
var link;
while(contents.hasNext()) {
file = contents.next();
link = file.getUrl();
linkoutput.setValue(link);
}
};
3 more things:
I`d like to have this script looped through the whole column, so it would check each folder name in column D and output files links in the column W;
the best outcome would be if it could output all the 3 links(as each folder would have only 3 files anyways) in the range of W:Y of the same row with the foder name;
and the last if this script could run automatically without a need to open Scripts tool and running the code every time.
thanks in advance!
For this question:
and the last if this script could run automatically without a need to open Scripts tool and running the code every time. thanks in advance!
You can set a Trigger to run when the spreadsheet is opened. Or you could set time triggers, to run daily for example.
Regarding the code, It looks like your are setting the link url always to the same cell (W60).

Get File Path From Drop Event

I have some javascript code that allows users to drag and drop files in IE.
The problem I have is that they would like the file deleted after being dragged, but I can't seem to get the file path, just the name.
I have seen many references to e.dataTransfer.files[0].path, but it always comes back as 'undefined' when I try it.
Any ideas why e.dataTransfer.files[0].path does not work, or how I can get the file path?
$(document).ready(function (ex) {
var holder = document.getElementById('holder');
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondrop = function (e) {
e.preventDefault();
var file = e.dataTransfer.files[0];
var path = e.dataTransfer.files[0].path;
fileArray.push(file);
//alert(e.target.id);
var reader = new FileReader();
reader.readAsDataURL(file);
};
});
that's not possible for a web app, but if your users would run an app on their machine, you could build an electron app. inside electron you get the e.dataTransfer.files[0].path property, which you can then use to delete the file.
https://www.electronjs.org/
The file doesn't have property path.
You can experiment with it here and the complete list of file properties can be found here.
To read the file one uses FileReader as you did, without using the path explicitly.
After trying everything I could find, I am of the opinion that it is not possible.

How to read the real time changes in a file with CasperJS

I am trying to let my CasperJS script read an outside txt file (abc.txt), and the abc.txt needed to be created in the middle of the CasperJS process.
(and that abc.txt has to be create using 'curl' for some third party api, so I called a childprocess to renew the old abc.txt).
However, the information inside abc.txt seems locked at the begging, it doesn't matter whether I changed the information or delete the whole file.
Has anyone any idea if CasperJS can be more interactive?
or any advice if I need to change the whole script?
(I am trying to get a question from websiteA, then go to websiteB find the answer, then submit the answer in websiteA)
var casper = require('casper').create();
var fs = require('fs');
var data = fs.read('abc.txt');
casper.start();
casper.wait(5000, function () {
console.log('wait 5000 for editing the file');
});
casper.then(function (){
console.log(data);
});
casper.run();
If the content of data.txt changes, during the execution of your script, this doesn't have any influence on variables that hold a copy of the content of that file. There is never a direct connection between the string value and the file content.
If you want to refresh the string content, you need to read that file again:
data = fs.read('abc.txt');
If you need to wait for the change of the file, then you can periodically read the file contents to see if they changed in the mean time. This can be done with casper.waitFor():
var casper = require('casper').create();
var fs = require('fs');
var data = fs.read('abc.txt');
var newData;
casper.start();
casper.waitFor(function _check(){
newData = fs.read('abc.txt');
return data !== newData;
});
casper.wait(1000); // additional wait to make sure that the file writing has finished
casper.then(function (){
console.log(newData);
});
casper.run();

Cannot write to file in Titanium

I am having trouble writing to a file in Titanium Studio.
specifically .json file. Code is compiled through and no exception was thrown.
Here is my relevant section of code, I parse the file to var first before adding element and stringify it to be written back.
Reading works perfectly, so is adding element, it's the writing process that has issues
var file = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'data.json');
var jsontext = file.read().toString();
var jsondoc = JSON.parse(jsontext);
jsondoc['feedlist'].push({
"picloc":imagename,
"title":titlef.value,
"desc1":descf1.value,
"desc2":descf2.value,
"desc3":descf3.value
});
jsontext = JSON.stringify(jsondoc);
file.write(jsontext); // write(data,[append])
Note: I have consulted Documentation and done some of my own search, some are suggesting that "Filestream" should be used in place of normal file along with .close(), I have yet got them working but it could be pointers the solution, if anyone knows how to get it working
Thanks in advance.
EDIT: This question is flagged for duplication, initially I deemed that was 2 separate issues, one was about merely writing text to a file. Another is parsing event.media (picture) into a file.
I got it working now, The issue was that I was trying to write to file in read-only directory
Ti.Filesystem.resourcesDirectory: A read-only directory where your application resources are located
Ti.Filesystem.applicationDataDirectory: A read/write directory accessible by your app. Place your application-specific files in this directory.
The contents of this directory persist
until you remove the files or until the user uninstalls the application
Here is my code, directory is modified
var sesfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'data2.json');
var jsontext = sesfile.read().toString();
var jsondoc = JSON.parse(jsontext);
jsondoc['feedlist'].push({
"picloc":imagename,
"title":titlef.value,
"desc1":descf1.value,
"desc2":descf2.value,
"desc3":descf3.value
});
jsontext = JSON.stringify(jsondoc);
sesfile.write(jsontext,false);
If you are unable to locate data directory and simply want to load the file from there.
(In my case it does not exist in project nor will be created with Webpreview compilings)
You can do bootstrap-ish type instruction like this first
var rdfile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'data.json');
var sesfile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory,'data2.json');
var jsontext = rdfile.read().toString();
var jsondoc = JSON.parse(jsontext);
sesfile.write(jsontext);
hope it helps whomever makes amateur mistake like I did.

phonegap code get the full path of a file to post in html

I am making a phonegap app for a offline books app. Here i am saving some files in a folder in the root of the device and display all the files in that folder for user to choose. when user clicks on a file to load it i am taking the full path of the file and giving to another javascript function to load it.
The files are saved in myappfolder in the root of the phone and i am getting the file path currectly.
My code is below:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function(fileSystem) {
fileSystem.root.getDirectory("myappfolder",
{
create: true
}, function(directory) {
var directoryReader = directory.createReader();
directoryReader.readEntries(function(entries) {
if(entries.length > 0 ) {
for (i=0; i<entries.length; i++) {
var name = entries[i].name;
var div1 = document.createElement('div');
div1.className = 'books';
div1.id = entries[i].fullPath;
div1.innerHTML = entries[i].name +'<hr>';
document.getElementById('content'). appendChild(div1);
div1.onclick =function (){
redirect(this.id);
}
}
}
When i use this code to load the file i get
network error in file://myappfolder/file.jpg
Kindly help me in solving this as i am new to phonegap.
I am passing this file to another js function which will display this file in a flip book format, It is working fine for me if i give a static file in the app folder
After lots of research i found the solution:
The fullPath gives the path only from the root directory, But if you give
entries[i].toNativeUrl
It is giving the complete path of the file The result of the above code is
file:///storage/emulated/0/myappfolder/name.pdf.
This result came in my phone(samsung galaxy s4)
it works fine in iphone also.
Hope this helps some one else.

Categories