How to fix string saving as code to word? - javascript

I have the following code:
function downloadNotes() {
var information = document.getElementById("text").innerHTML;
var textToBLOB = new Blob([information], { type: 'text/plain' });
var sFileName = 'formData = document.doc';
var newLink = document.createElement("a");
newLink.download = sFileName;
if (window.webkitURL != null) {
newLink.href = window.webkitURL.createObjectURL(textToBLOB);
}
else {
newLink.href = window.URL.createObjectURL(textToBLOB);
newLink.style.display = "none";
document.body.appendChild(newLink);
}
newLink.click();
}
When I save my notes, it successfully saves it to word, but when I open it, it shows the code all compressed rather than the string output:
Here.

Change this line:
var information = document.getElementById("text").innerHTML;
To this:
var information = document.getElementById("text").innerText;
Your code was reading the HTML content of the element instead of the Text value of the element. If this doesn't work you may need to code it to cut out the HTML.

Related

Modifying a JS browser extension's HTML parser to exclude images

I'll get out of the way that although I have a programming background I have no experience with any of the tools or languages I'm working with here. Sorry if I've made simple misunderstandings or communicate something unclearly.
I'm using an extension to generate EPUBs from website links. The extension has the option to customize the parsing logic from the options dialog. Doing so yields this code for the parser it's currently using:
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
var new_link = null;
var subchaps = [];
// Wordpress content
var main_cont = dom.querySelector(".entry-content")
if(main_cont != null){
var ancs = main_cont.querySelectorAll("a");
ancs.forEach((element) => {
if(RegExp(/click here to read|read here|continue reading/i).test(element.innerText)){
new_link = helpers["link_fixer"](element.href, url);
} else if (RegExp(/chapter|part/i).test(element.innerText)) {
subchaps.push(helpers["link_fixer"](element.href, url))
}
});
}
if (new_link != null) {
var res = await fetch(new_link);
var r_txt = await res.text();
dom = parser.parseFromString(r_txt, "text/html");
var out = helpers["readability"](dom);
return {title: out.title, html: out.content};
} else if (subchaps.length > 0) {
var html = "";
for(var subc in subchaps){
console.log(subchaps[subc]);
var cres = await fetch(subchaps[subc]);
var c_txt = await cres.text();
var cdom = parser.parseFromString(c_txt, "text/html");
var out = helpers["readability"](cdom);
html += "<h1>"+out.title+"</h1>"+ out.content
}
return {title: title, html: html};
}
var out = helpers["readability"](dom);
return {title: out.title, html: out.content};
I've inspected this code and gathered that it handles three cases: two ways that it needs to follow links deeper before parsing, and the simple case where it is already in the right place. The lions share of the code deals with the first two cases and it's largely the third that I'm interested in. Unfortunately, it appears that the second to last line is where the parsing actually happens:
var out = helpers["readability"](dom);
And this is a magic box to me. I can't for the life of me figure out what this is referencing.
I've searched the full file for a definition of 'helpers' or even 'readability' but come up blank. I was under the impression that the part I was editing was the 'readibility' parser. I thought I'd be able to pop into the parser logic, add a line to exclude nodes with the <img> tag, and live happily ever after. What am I mistaken about? Or is what I want to do impossible, given what the extension is letting me modify?
To be clear, I am not asking for a full guide on how to write parser logic. I considered just parsing and repackaging the document before the line in question, but I didn't want to write the same code 3 times, and I don't like that I can't tell what it's doing. I couldn't even begin to search for documentation, given that I can't find the definition in the first place. Even just explaining what that line does and pointing to any relevant documentation would be a great help.
Thanks in advance.
(And here is the full file, if you feel like verifying that I didn't miss anything.)
main_parser: |
var link = new URL(url);
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
switch (link.hostname) {
case "www.novelupdates.com":
var paths = link.pathname.split("/");
if (paths.length > 1 && paths[1] == "series") {
return {page_type:"toc", parser:"chaps_nu"};
}
}
// Default to all links
return {page_type:"toc", parser:"chaps_all_links"};
toc_parsers:
chaps_nu:
name: Novel Update
code: |
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
var chap_popup = dom.querySelector("#my_popupreading");
if (chap_popup == null) {
return []
}
var chap_lis = chap_popup.querySelectorAll("a");
var chaps = [];
chap_lis.forEach((element) => {
if (element.href.includes("extnu")) {
chaps.unshift({
url_title: element.innerText,
url: helpers["link_fixer"](element.href, url),
});
}
});
var tit = dom.querySelector(".seriestitlenu").innerText;
var desc = dom.querySelector("#editdescription").innerHTML;
var auth = dom.querySelector("#authtag").innerText;
var img = dom.querySelector(".serieseditimg > img");
if (img == null){
img = dom.querySelector(".seriesimg > img");
}
return {"chaps":chaps,
meta:{title:tit, description: desc, author: auth, cover: img.src, publisher: "Novel Update"}
};
chaps_name_search:
name: Chapter Links
code: |
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
var ancs = dom.querySelectorAll("a");
var chaps = []
ancs.forEach((element) => {
if(RegExp(/chap|part/i).test(element.innerText)){
chaps.push({
url_title: element.innerText,
url: helpers["link_fixer"](element.href, url),
});
}
});
return {"chaps":chaps,
meta:{}
};
chaps_all_links:
name: All Links
code: |
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
var ancs = dom.querySelectorAll("a");
var chaps = []
ancs.forEach((element) => {
chaps.push({
url_title: element.innerText,
url: helpers["link_fixer"](element.href, url),
});
});
return {"chaps":chaps,
meta:{}
};
chap_main_parser: |
var url = new URL(url);
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
// Generic parser
return {chap_type: "chap", parser:"chaps_readability"};
chap_parsers:
chaps_readability:
name: Readability
code: |
var parser = new DOMParser();
var dom = parser.parseFromString(source, "text/html");
var new_link = null;
var subchaps = [];
// Wordpress content
var main_cont = dom.querySelector(".entry-content")
if(main_cont != null){
var ancs = main_cont.querySelectorAll("a");
ancs.forEach((element) => {
if(RegExp(/click here to read|read here|continue reading/i).test(element.innerText)){
new_link = helpers["link_fixer"](element.href, url);
} else if (RegExp(/chapter|part/i).test(element.innerText)) {
subchaps.push(helpers["link_fixer"](element.href, url))
}
});
}
if (new_link != null) {
var res = await fetch(new_link);
var r_txt = await res.text();
dom = parser.parseFromString(r_txt, "text/html");
var out = helpers["readability"](dom);
return {title: out.title, html: out.content};
} else if (subchaps.length > 0) {
var html = "";
for(var subc in subchaps){
console.log(subchaps[subc]);
var cres = await fetch(subchaps[subc]);
var c_txt = await cres.text();
var cdom = parser.parseFromString(c_txt, "text/html");
var out = helpers["readability"](cdom);
html += "<h1>"+out.title+"</h1>"+ out.content
}
return {title: title, html: html};
}
var out = helpers["readability"](dom);
return {title: out.title, html: out.content};
chaps_raw:
name: No Parse
code: |
return {title: title, html: source}

JavaScript: The image attachment is added directly from the clipboard with the key combination ctrl + v

I have a task I don't know how to start. Please tell me, if there's too much code attached or if I can get any clues from old questions. Am I missing code ony in one of the files below or both?
In the client page there's only possible to drag and drop the attachment, or click square or link and then choose any files from the computer.
Task description is this:
"Purpose and need: The image attachment is added directly from the clipboard with ctrl + v.
The server has an interface at xxxxx.com. Attachments are sent on PUT request and the query portion of the address is, for example, like ?attachment_id[/request_id/urlkey], where attachment_id is something unique and invented, request_id is the request ID number, and urlkey is the "password" associated the request. The last two are therefore either defined or not at all. The content of the transmission itself must be a multipart/form-data model.
The server then returns a JSON response and if it was a success then the okMessage field can be found in it immediately at the root. Otherwise, an error message such as errorMessages (str arr) or errors (obj → srt) will return.
There are two places on this page to send an attachment:
Make a New Job Request. See if this is visible. Here's how to check it: document.getElementById('v1r').checked. If the file was successfully uploaded to the server, it will add a tag as an extension to this table: document.getElementById ('rootNew')._fields.appendixTags.i.a_newTags.
Previously sent request. See if this is visible. Here's how: document.getElementById('v2r').checked && document.getElementById ('justRequest') parentElement.style.display != 'nothing'
(v1r and v2r are mutually exclusive, meaning only one can be visible at a time) Then, when the file has been successfully uploaded, even if you call this function, it will already be the text you want to see: l_help_addFileList(lt, name, size, mime, days) however, only name information is currently used. But where lt comes from in 1, is document.getElementById('justNew')._fields.appendixTags.i.e_sentFiles, and the conditions in section 2 are: document.getElementById('requestAttachment').e_sentFiles.
Job Request Response (Email) Not yet
In step 2, the credentials can be angled from the tag_id and tag_key data stored in the document.getElementById ('comment') element."
I have no idea where to start. I have two javascript files: index and sendAttachment. First of all, must I modify both files or only sendAttachment one?
Secondly, what I'm missing from the code so that in addition to dragging and droping , attachments can also be attached with ctrl + v?
I appreciate your help. Thank you!
Here's the current codes from client side:
index.js
//SOME CODE FIRST
//Send button
{
let send = document.createElement('div');
send.id = 'send';
send.className = 'tr';
let button = document.createElement('input');
button.type = 'button';
button.onclick = new_sendForm;
button.value = 'Submit';
send.appendChild(button);
root.appendChild(send);
root._send = button;
}
//Shows that notification is received
{
let note = document.createElement('div');
note.id = 'new_note';
note.style.display = 'none';
let div = document.createElement('div');
let text = document.createElement('h2');
text.textContent = 'Sended successfully';
div.appendChild(text);
let button = document.createElement('input');
button.type = 'button';
button.onclick = function reset(){
note.style.display = 'none';
new_resetForm();
if (root._fields.attachmentTags)
l_addBufferedAttachmentInformation(root._fields.attachmentTags.i, []);
};
button.value = 'Empty form';
div.appendChild(button);
div.appendChild(document.createElement('br'));
div.appendChild(document.createElement('br'));
let p = document.createElement('i');
p.textContent = 'You can find this and previous requests from the tab named Previous'
+ ' if you gave your email address.'
;
div.appendChild(p);
p = document.createElement('p');
p.style.fontFamily = "'Lucida Console', monospace";
p.innerHTML = 'Here's the link<br>';
let a = document.createElement('a');
a.href = '#';
a.textContent = 'The link isn't created yet';
p.appendChild(a);
div.appendChild(p);
note.appendChild(div);
root.appendChild(note);
root._note = note;
root._note_a = a;
}
{
let email = root._fields['email'];
let o = s_get();
if (email && email.i && !email.i.value && o.email)
email.i.value = o.email;
}
}
// ----------------------------------------------------------------------------
function show_comment(){
let comment = document.getElementById('comment');
let selection = null;{
let inputs = comment.getElementsByTagName('input');
for (let i=0, ii=inputs.length; i<ii; i++){
let input = inputs.item(i);
if (input.checked){
selection = input.value;
break;
}
}
}
let n_comment = document.getElementById('n_comment');
let n_comment_error = document.getElementById('n_comment_error');
let n_comment_button = document.getElementById('n_comment_button');
if (!selection)
n_comment_error.textContent = 'Not done';
else if (!n_comment.value.trim())
n_comment_error.textContent = 'Comment missing';
else {
n_comment_error.textContent = '';
n_comment_button.disabled = true;
let post = {
feature: "addComment",
getRequest: {
id: comment.key_id,
urlkey: comment.key_key,
status: selection,
description: n_comment.value
}
}
xhr(
function ok(json){
n_comment_button.disabled = false;
if (json.errorMessages)
n_comment_error.innerHTML = json.errorMessages.join('<br>');
else
n_comment.value = '';
if (json.requests && json.requests[0]){
let request = json.requests[0];
let label = document.getElementById('rootRequest').getElementsByTagName('h3').item(0);
if (label && request.jira_fields && request.jira_fields.description){
label.nextElementSibling.innerHTML = '';
doc2html(request.jira_fields.description, bella.nextElementSibling);
}
}
},
'/api',
JSON.stringify(post),
function err(json){
n_comment_button.disabled = false;
n_comment_error.textContent = 'Sending failed';
}
);
}
}
//OTHER CODES BEFORE NEXT ONES:
//Attachments part
let requestAttachment = document.getElementById('requestAttachment');
if (request.jira_fields && request.jira_fields.attachment)
l_addJiraAttachmentInfo(requestAttachment, request.jira_fields.attachment);
else if (pyynto.attachmentTags)
l_addBufferedAttachmentInfo(requestAttachment, request.attachmentTags);
else
l_addBufferedAttachmentInfo(requestAttachment, []);
} else {
root.innerHTML = 'Didn't find the request';
}
root.parentElement.style.display = 'inline-block';
}
function show_get(id, urlkey){
xhr(
function ok(json){
show_show(json);
},
'/api',
JSON.stringify({
feature: 'get',
getRequest: {
id: id,
urlkey: urlkey
}
}),
function err(json){
let root = document.getElementById('rootRequest');
root.innerHTML = '<b>Download failed</b>';
}
);
}
function show_initLomake(){
let get = getParameters();
let root = document.getElementById('rootRequest');
if (get['id'] && get['idKey']){
root.innerHTML = '<i>Searching...</i>';
show_get(get['id'], get['idKey']);
//Switching page
document.getElementById('v2r').checked = true;
} else {
root.parentElement.style.display = 'none';
}
let requestAttachment = document.getElementById('requestAttachment');
let comment = document.getElementById('comment');
if (typeof l_createForm === 'function' && requestAttachment && comment)
l_createForm(requestAttachment, comment);
else if (comment)
requestAttachment.parentElement.style.display = 'none';
}
function list_sendLink(email){
if (email){
xhr(
function ok(json){
let root = document.getElementById('rootList');
if (json.okMessage)
root.textContent = json.okMessage;
else if (json.errors && json.errors['sendMssage']){
let div = document.createElement('div');
div.textContent = json.errors['sendMessage'];
div.className = 'error';
root.appendChild(div);
} else if (json.errorMessages && json.errorMessages[0]){
let div = document.createElement('div');
div.textContent = json.errorMessages[0];
div.className = 'error';
root.appendChild(div);
} else
root.innerHTML = "Something went wrong. Don't know what or where.";
},
'/api',
JSON.stringify({
feature: 'sendListTag',
newForm: {
email: email
}
}),
function err(json){
let root = document.getElementById('rootList');
let div = document.createElement('div');
div.textContent = 'Sending the request to server failed???';
div.className = 'error';
root.appendChild(div);
}
);
}
}
function list_initShowPrevious(){
let get = getParameters();
let store = s_get();
let root = document.getElementById('rootList');
let mm_list = document.getElementById('mm_list');
let email = get['email'];
let listTag = get['list'];
if (store.email && store.listTag){
s_forgetButton(true);
if (!email || !listag || store.email == email && store.listTag == listTag){
email = store.email;
listTag = store.listTag;
mm_list.innerHTML = 'Email and list tags.';
} else {
mm_list.innerHTML = 'Email and list tag, but they differ!<br><i>If you want to update, you must forget old infos first.</i>';
}
} else if (email && listTag){
s_post(email, listTag);
mm_list.innerHTML = 'Email and list tags (saved now).';
s_forgetButton(true);
} else {
mm_list.innerHTML = 'Nothing';
}
if (email && listTag){
root.innerHTML = '<i>Searching...</i>';
list_get(email, listTag);
if (!get['form'])
document.getElementById('v2r').checked = true;
}
}
// ----------------------------------------------------------------------------
function init(){
//New form?
xhr(
function settingsGot(json){
new_initForm(json);
window._settings = json;
list_initShowPrevious();
show_initForm();
},
'form.new.json',
null,
null
);
}
sentAttachment.js
'use strict';
function l_createForm(root, settingSource){
if (!root || !ss || !ss.SimpleUpload){
console.error("Formatting of downloading attachment file failed.");
} else {
root.a_settingSource = settingSource;
root.a_newTags = [];
{
let sendedFiles = document.createElement('fieldset');
root.e_SendedFiles = sendedFiles;
sendedFiles.className = 'sendedFiles';
let legend = document.createElement('legend');
legend.innerHTML = 'Sended files';
sendedFiles.appendChild(legend);
root.appendChild(sendedFiles);
}
{
let progressBox = document.createElement('div');
root.e_progressBox = progressBox;
progressBox.className = 'progressBox';
root.appendChild(progressBox);
}
{
let key = document.createElement('p');
root.e_key = key;
key.className = 'key';
key.innerHTML = '<span>Drag and drop the files you want to send to the square above</span> ' +
'or <a>click here</a>';
root.e_clicking = key.lastElementChild;
root.appendChild(key);
}
root.e_fileUpload = new ss.SimpleUpload({
button: [root.e_sendedFiles, root.e_clicking],
url: function before(xhr, settings){
let extra = '';
if (settingSource && settingSource.tag_id && settingSource.tag_key)
extra = '/' + settingSource.tag_id + '/' + settingSource.tag_key;
return location.protocol +
'//' + location.host +
'/api?' +
(Date.now()) +
extra
;
},
name: "file",
method: "PUT",
dropzone: root,
dragClass: 'fileOn',
encodeHeaders: true,
cors: false,
multiple: true,
multipleSelect: true,
noParams: true,
multipart: true,
autoSubmit: true,
responseType: "json",
debug: false,
onDone: function sendingPassed(
fileName,//String
statusCode,//the response status code, if any
statusText,//the response status code, if any
json,//false
button,//Button which started sending
fileSize//number or null
){
let sendedFiles = root.e_sendedFiles;
if (sendedFiles){
l_help_addFileToList(
sendedFiles,
fileName,
fileSize,
null,
null
);
if (json && json.attachmentTag)
root.a_newTags.push(json.attachmentTag);
} else {
alert('File "' + fileName + '" sended successfully.');
}
},
onError: function sendingError(
fileName,//String
errorType,//"error" or "parseerror"
statusCode,//the response status code, if any
statusText,//the response status code, if any
answer,//false
button,//Button which started sending
fileSize//number or null
){
alert('"' + fileName + '": ' + statusText);
},
onSubmit: function(filename, extension) {
// Create the elements of our progress bar
var progress = document.createElement('div'), // container for progress bar
bar = document.createElement('div'), // actual progress bar
fileSize = document.createElement('div'), // container for upload file size
wrapper = document.createElement('div'), // container for this progress bar
//declare somewhere: <div id="progressBox"></div> where you want to show the progress-bar(s)
progressBox = root.e_progressBox; //on page container for progress bars
// Assign each element its corresponding class
progress.className = 'progress progress-striped';
bar.className = 'progress-bar progress-bar-success';
fileSize.className = 'size';
wrapper.className = 'wrapper';
// Assemble the progress bar and add it to the page
progress.appendChild(bar);
wrapper.innerHTML = '<div class="name">Sending <b>'+filename+'</b></div>'; // filename is passed to onSubmit()
wrapper.appendChild(fileSize);
wrapper.appendChild(progress);
progressBox.appendChild(wrapper); // just an element on the page to hold the progress bars
// Assign roles to the elements of the progress bar
this.setProgressBar(bar); // will serve as the actual progress bar
this.setFileSizeBox(fileSize); // display file size beside progress bar
this.setProgressContainer(wrapper); // designate the containing div to be removed after upload
}
});
}
}
function l_help_addFileToList(root, name, size, mime, date){
let square = document.createElement('div');
square.className = 'square';
let text = document.createElement('span');
text.className = 'name';
text.innerHTML = name;
square.appendChild(text);
/*
let size = document.createElement('i');
size.className = 'size';
size.innerHTML = size;
square.appendChild(size);
*/
root.appendChild(square);
}
function l_addJiraAttachmentInfo(root, attachment_arr){
if (root.e_sendedFiles && attachment_arr instanceof Array){
root.e_sendedFiles.innerHTML = '';
root.a_newTags = [];
for (let i=0, ii=attachment_arr.length; i<ii; i++){
let a = attachment_arr[i];
l_help_addFileToList(
root.e_sendedFiles,
a.filename,
a.size,
a.mimeType,
a.created
);
}
}
}
function l_addBufferedAttachmentInfo(root, attachmentTags){
if (root.e_sendedFiles && attachmentTags instanceof Array){
root.e_sendedFiles.innerHTML = '';
root.a_newTags = [];
for (let i=0, ii=attachmentTags.length; i<ii; i++){
let a = attachmentTags[i];
l_help_addFileToList(
root.e_sendedFiles,
'Sending_' + a,
-1,
null,
null
);
}
}
}
function l_getSendedTags(root){
if (root.a_newTags)
return root.a_newTags;
else
return null;
}

remove blank lines when uploading csv files angular

I got a csv-reader directive and let's user upload a csv file. I noticed that when I upload a file with spaces between words for example:
abc
abc
abc
abc
abc
this gets shown. I want to delete all the blank lines Not sure what to do.
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
var rows = contents.split('\n');
// Check if the last row is empty. This works
if(rows[rows.length-1] ===''){
rows.pop()
}
}
// this doesn't work for some reason. It doesn't detect the '' in the middle of the arrays.
for( var i=rows.length-1;i>0;i--){
if(rows[i] === ''){
rows.splice(i,1)
}
}
Try using Array.prototype.filter()
var rows = contents.split('\n').filter(function(str){
return str;
});
From what you have shown it looks like you want to check if each item in the csvModel is an empty string, rather than newValue
Something like:
for( var i=0 ;i< $scope.csvModel.length; i++){
if (csvModel[i] == "") {
$scope.csvModel.splice(i,1);
}
}
var text = [];
var target = $event.target || $event.srcElement;
var files = target.files;
if(Constants.validateHeaderAndRecordLengthFlag){
if(!this._fileUtil.isCSVFile(files[0])){
alert("Please import valid .csv file.");
this.fileReset();
}
}
var input = $event.target;
var reader = new FileReader();
reader.readAsText(input.files[0], 'UTF-8');
reader.onload = (data) => {
let csvData = reader.result;
let csvRecordsArray = csvData.split(/\r\n|\n/);
if (csvRecordsArray[csvRecordsArray.length - 1] === '') {
csvRecordsArray.pop();
}
var headerLength = -1;
if(Constants.isHeaderPresentFlag){
let headersRow = this._fileUtil.getHeaderArray(csvRecordsArray, Constants.tokenDelimeter);
headerLength = headersRow.length;
}
this.csvRecords = this._fileUtil.getDataRecordsArrayFromCSVFile(csvRecordsArray,
headerLength, Constants.validateHeaderAndRecordLengthFlag, Constants.tokenDelimeter);
if(this.csvRecords===null){
this.csvRecords=[];
}
else if(this.csvRecords!==null) {
if ((JSON.stringify(this.csvRecords[0])) === (JSON.stringify(this.csvFormate))) {
alert("format matches");
this.displayCsvContent = true;
for (let i = 0; i < this.csvRecords.length; i++) {
if (i !== 0) {
this.csvRecords[i].push(this.recordInsertedFlag);
}
}
}
else {
alert("format not matches");
}
}
if(this.csvRecords == null){
this.displayCsvContent=false;
//If control reached here it means csv file contains error, reset file.
this.fileReset();
}
};
reader.onerror = function () {
alert('Unable to read ' + input.files[0]);
};

Explanation of phantomjs code

I was asked to work on web crawler using phantomjs. However, as I read through the example, I was puzzled by some of the code:
Is this a loop? $("table[id^='post']").each(function(index)
What does this line of code mean? var entry = $(this);
How is the id captured? var id = entry.attr('id').substring(4);
This line var poster = entry.find('a.bigusername'); tries to get a username from the page. Is there a tutorial on how to make use of entry.find to scrap data off the page?
page.open(url1, function (status) {
// Check for page load success
if (status !== "success") {
console.log("Unable to access network");
phantom.exit(231);
} else {
if (page.injectJs("../lib/jquery-2.1.0.min.js") && page.injectJs("../lib/moment-with-langs.js") && page.injectJs("../lib/sugar.js") && page.injectJs("../lib/url.js")){
allResults = page.evaluate(function(url) {
var arr = [];
var title = $("meta[property='og:title']").attr('content');
title = title.trim();
$("table[id^='post']").each(function(index){
var entry = $(this);
var id = entry.attr('id').substring(4);
var poster = entry.find('a.bigusername');
poster = poster.text().trim();
var text = entry.find("div[id^='post_message_']");
//remove quotes of other posts
text.find(".quote").remove();
text.find("div[style='margin:20px; margin-top:5px; ']").remove();
text.find(".bbcode_container").remove();
text = text.text().trim();
var postDate = entry.find("td.thead");
postDate = postDate.first().text().trim();
var postUrl = entry.find("a[id^='postcount']");
if (postUrl){
postUrl = postUrl.attr('href');
postUrl = URL.resolve(url, postUrl);
}
else{
postUrl = url;
}
if (postDate.indexOf('Yesterday') >= 0){
postDate = Date.create(postDate).format('{yyyy}-{MM}-{dd} {HH}:{mm}');
}
else if (postDate.indexOf('Today') >= 0){
postDate = Date.create(postDate).format('{yyyy}-{MM}-{dd} {HH}:{mm}');
}
else{
var d = moment(postDate, 'DD-MM-YYYY, hh:mm A');
postDate = d.format('YYYY-MM-DD HH:mm');
}
var obj = {'id': id, 'title': title, 'poster': poster, 'text': text, 'url': postUrl, 'post_date' : postDate, 'location': 'Singapore', 'country': 'SG'};
arr.push(obj);
});
return arr;
}, url);
console.log("##START##");
console.log(JSON.stringify(allResults, undefined, 4));
console.log("##END##");
console.log("##URL=" + url);
fs.write("../cache/" + encodeURIComponent(url), page.content, "w");
phantom.exit();
}
}
});
Is this a loop? $("table[id^='post']").each(function(index)?
Yes
What does this line of code mean? var entry = $(this);
It assigns a jQuery object to variable entry
How is the id captured? var id = entry.attr('id').substring(4);
It uses jQuery which has attr() function.

javascript parser for a string which contains .ini data

If a string contains a .ini file data , How can I parse it in JavaScript ?
Is there any JavaScript parser which will help in this regard?
here , typically string contains the content after reading a configuration file. (reading cannot be done through javascript , but somehow I gather .ini info in a string.)
I wrote a javascript function inspirated by node-iniparser.js
function parseINIString(data){
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
var value = {};
var lines = data.split(/[\r\n]+/);
var section = null;
lines.forEach(function(line){
if(regex.comment.test(line)){
return;
}else if(regex.param.test(line)){
var match = line.match(regex.param);
if(section){
value[section][match[1]] = match[2];
}else{
value[match[1]] = match[2];
}
}else if(regex.section.test(line)){
var match = line.match(regex.section);
value[match[1]] = {};
section = match[1];
}else if(line.length == 0 && section){
section = null;
};
});
return value;
}
2017-05-10 updated: fix bug of keys contains spaces.
EDIT:
Sample of ini file read and parse
You could try the config-ini-parser, it's similar to python ConfigParser without I/O operations
It could be installed by npm or bower. Here is an example:
var ConfigIniParser = require("config-ini-parser").ConfigIniParser;
var delimiter = "\r\n"; //or "\n" for *nux
parser = new ConfigIniParser(delimiter); //If don't assign the parameter delimiter then the default value \n will be used
parser.parse(iniContent);
var value = parser.get("section", "option");
parser.stringify('\n'); //get all the ini file content as a string
For more detail you could check the project main page or from the npm package page
Here's a function who's able to parse ini data from a string to an object! (on client side)
function parseINIString(data){
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
var value = {};
var lines = data.split(/\r\n|\r|\n/);
var section = null;
for(x=0;x<lines.length;x++)
{
if(regex.comment.test(lines[x])){
return;
}else if(regex.param.test(lines[x])){
var match = lines[x].match(regex.param);
if(section){
value[section][match[1]] = match[2];
}else{
value[match[1]] = match[2];
}
}else if(regex.section.test(lines[x])){
var match = lines[x].match(regex.section);
value[match[1]] = {};
section = match[1];
}else if(lines.length == 0 && section){//changed line to lines to fix bug.
section = null;
};
}
return value;
}
Based on the other responses i've modified it so you can have nested sections :)
function parseINI(data: string) {
let rgx = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([^=]+?)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
let result = {};
let lines = data.split(/[\r\n]+/);
let section = result;
lines.forEach(function (line) {
//comments
if (rgx.comment.test(line)) return;
//params
if (rgx.param.test(line)) {
let match = line.match(rgx.param);
section[match[1]] = match[2];
return;
}
//sections
if (rgx.section.test(line)) {
section = result
let match = line.match(rgx.section);
for (let subSection of match[1].split(".")) {
!section[subSection] && (section[subSection] = {});
section = section[subSection];
}
return;
}
});
return result;
}

Categories

Resources