passing page scraped data in the URL - javascript
In my Chrome extension, I'm trying to scrape information from the current tab (in content.js) and send it as parameter to the provided URL (background.js). It seems like I can scrape everything from the tab and append it to the URL except the values of input tags. Here's my code:
content.js:
var elements = new Array("form","h1","input","td","textarea","time","title","var");
//declare an array for found elements
var foundElements = new Array();
//declare an array for found ids
var foundIds = new Array();
//this counter is used to hold positions in the element array.
var elementCounter = 0;
//this counter is used to hold positions in the foundIds array
var idsCounter = 0;
//this counter is used to hold positions in the classCounter array.
var classCounter = 0;
//and we're going to output everything in a giantic string.
var output = "URL=" + document.URL;
//scrape the page for all elements
for (var i = 0; i < elements.length; i++)
{
var current = document.getElementsByTagName(elements[i]);
if(current.length>0)
{
for (var z=0; z<current.length; z++)
{
var inTxt = current[z].innerText;
output += "&" + elements[i] + "=" + inTxt;
}
elementCounter++;
//now that we have an array of a tag, check it for IDs and classes.
for (var y = 0; y<current.length; y++)
{
//check to see if the element has an id
if(current[y].id)
{
//these should be unique
var hit = false;
for (var x = 0; x<foundIds.length; x++)
{
if(foundIds[x]==current[y].id)
{
hit=true;
}
}
//if there was no hit...
if(!hit)
{
foundIds[idsCounter]=current[y].id;
idsCounter++;
var currVal = current[y].value;
output+="&" + current[y].id + "=" + currVal;
}
}
//now we pull the classes
var classes = current[y].classList;
if(classes.length>0)
{
for (var x = 0; x<classes.Length; x++)
{
var hit = false;
for (var z = 0; z<foundClasses.length; z++)
{
if(foundClasses[z]==classes[x])
{
hit=true;
}
}
//if there was not a hit
if(!hit)
{
foundClasses[classCounter]=classes[x];
classCounter++;
output+="&" + classes[x] + "=";
}
}
}
}
}
}
chrome.runtime.sendMessage({data: output});
background.js:
var output2;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
output2 = "text_input1=";
output2 += request.data;
});
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.create({url: "http://www.google.com?" + output2}, function(tab) {
chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() {
sendMessage();
});
});
});
Does anyone know why the input tags values are passed as blank?
Because you're trying to get the input text by using current[z].innerText.
However, you have to use current[z].value for inputs.
Related
Why I get Indefined when displaying Img’s on page?
I have been playing with this code for a while and I was wondering why when I try to add img’s to the array on the js code makes the images appear on DOM but also makes a bunch of Undefined elements appear, How can I just make the 15 images appear without the undefined? Thanks enter link description here var previous = document.getElementById('btnPrevious') var next = document.getElementById('btnNext') var gallery = document.getElementById('image-gallery') var pageIndicator = document.getElementById('page') var galleryDots = document.getElementById('gallery-dots'); var images = ["https://exoplanets.nasa.gov/internal_resources/1763/", "https://cdn.britannica.com/56/234056-050-0AC049D7/first-image-from-James-Webb-Space-Telescope-deepest-and-sharpest-infrared-image-of-distant-universe-to-date-SMACS-0723.jpg", "https://assets.newatlas.com/dims4/default/ac389ce/2147483647/strip/true/crop/1620x1080+150+0/resize/1200x800!/quality/90/?url=http%3A%2F%2Fnewatlas-brightspot.s3.amazonaws.com%2Farchive%2Funiverse-expanding-acceleration-1.jpg", "https://media.newyorker.com/photos/590966ee1c7a8e33fb38d6cc/master/w_2560%2Cc_limit/Nissan-Universe-Shouts.jpg", "https://www.thoughtco.com/thmb/NY5k_3slMRttvtS7mA0SXm2WW9Q=/1500x0/filters:no_upscale():max_bytes(150000):strip_icc()/smallerAndromeda-56a8ccf15f9b58b7d0f544fa.jpg", "https://static.scientificamerican.com/sciam/cache/file/05B8482C-0C04-4E41-859DCCED721883D2_source.jpg?w=590&h=800&7ADE2895-F6E3-4DF4-A11F51B652E9FA88", "https://qph.cf2.quoracdn.net/main-thumb-66277237-200-huqebnzwetdsnnwvysbxemlskpcxnygf.jpeg", "http://www.pioneertv.com/media/1090/hero_shot_1080x720.jpg?anchor=center&mode=crop&width=600&height=400&rnd=133159257140000000", "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRSWFW1EpMNFM5-dbZEUUnzJkzT3KbUCeuhPHx_eseFCpPeX4Q_DIVPopjS0LeKVmKdQho&usqp=CAU", "https://cdn.mos.cms.futurecdn.net/rwow8CCG3C3GrqHGiK8qcJ.jpg", "https://static.wixstatic.com/media/917d103965314e2eacefed92edb6492c.jpg/v1/fill/w_640,h_356,al_c,q_80,usm_0.66_1.00_0.01,enc_auto/917d103965314e2eacefed92edb6492c.jpg", "https://astronomy.com/~/media/A5B9B6CF36484AB9A6FFAE136C55B355.jpg", "https://discovery.sndimg.com/content/dam/images/discovery/fullset/2022/9/alien%20planet%20GettyImages-913058614.jpg.rend.hgtvcom.616.411.suffix/1664497398007.jpeg", "https://images.newscientist.com/wp-content/uploads/2017/06/21180000/planet-10-orange-blue-final-small.jpg?crop=16:9,smart&width=1200&height=675&upscale=true", "https://images.hindustantimes.com/img/2022/07/20/1600x900/Viral_Instagram_Planet_Rainbow_Nasa_1658316556293_1658316573815_1658316573815.PNG" ] for (var i = 0; i < 15; i++) { images.push({ title: "Image " + (i + 1), source: images[i] }); } var perPage = 8; var page = 1; var pages = Math.ceil(images.length / perPage) // Gallery dots for (var i = 0; i < pages; i++){ var dot = document.createElement('button') var dotSpan = document.createElement('span') var dotNumber = document.createTextNode(i + 1) dot.classList.add('gallery-dot'); dot.setAttribute('data-index', i); dotSpan.classList.add('sr-only'); dotSpan.appendChild(dotNumber); dot.appendChild(dotSpan) dot.addEventListener('click', function(e) { var self = e.target goToPage(self.getAttribute('data-index')) }) galleryDots.appendChild(dot) } // Previous Button previous.addEventListener('click', function() { if (page === 1) { page = 1; } else { page--; showImages(); } }) // Next Button next.addEventListener('click', function() { if (page < pages) { page++; showImages(); } }) // Jump to page function goToPage(index) { index = parseInt(index); page = index + 1; showImages(); } // Load images function showImages() { while(gallery.firstChild) gallery.removeChild(gallery.firstChild) var offset = (page - 1) * perPage; var dots = document.querySelectorAll('.gallery-dot'); for (var i = 0; i < dots.length; i++){ dots[i].classList.remove('active'); } dots[page - 1].classList.add('active'); for (var i = offset; i < offset + perPage; i++) { if ( images[i] ) { var template = document.createElement('div'); var title = document.createElement('p'); var titleText = document.createTextNode(images[i].title); var img = document.createElement('img'); template.classList.add('template') img.setAttribute("src", images[i].source); img.setAttribute('alt', images[i].title); title.appendChild(titleText); template.appendChild(img); template.appendChild(title); gallery.appendChild(template); } } // Animate images var galleryItems = document.querySelectorAll('.template') for (var i = 0; i < galleryItems.length; i++) { var onAnimateItemIn = animateItemIn(i); setTimeout(onAnimateItemIn, i * 100); } function animateItemIn(i) { var item = galleryItems[i]; return function() { item.classList.add('animate'); } } // Update page indicator pageIndicator.textContent = "Page " + page + " of " + pages; } showImages();
I checked your code and make it work with a small modification. You are reusing the same array with the links of images and push inside the new object with the shape of { title, source }. You just need to do this changes: Change the name of your array of images. Something from images to arrayOfImages. const arrayOfImages = ["https://exoplanets.nasa.gov/internal_resources/1763/", ...] Declare an empty array before your first for loop. Make something like const images = [] On your for loop, instead of loop over the images variable, do it over the arrayOfImages variable. const images = []; for (var i = 0; i < 15; i++) { images.push({ title: "Image " + (i + 1), source: arrayOfImages[i] }); } With those changes, everything works for me. Also, as a recommendation, try to avoid the var keyword. If you want more details about this, this answer is very helpful: https://stackoverflow.com/a/50335579/17101307
You can use Array#map to create a new array of objects from the array of URLS, then replace the original array. images = images.map((x, i) => ({ title: "Image " + (i + 1), source: x }));
Getting the inner html from an iFrame
I have a function that is intended to count the occurrences of each word on a web page (from the same domain) I've read numerous posts about getting the innerhtml of an iframe, however I think I'm misunderstanding as this isn't working, and is just giving me an empty string. TagID is the location in my HTML doc I want to add these items to. function filereader(tagID) { var x = document.getElementById(tagID); var newFrame = document.createElement("iframe"); newFrame.id = "pageinframe"; newFrame.src = document.getElementById("pagetoread").value; x.appendChild(newFrame); var innerDoc = document.getElementById("pageinframe"); var innerDocContent = innerDoc.contentDocument.body.innerHTML; console.dir(innerDocContent); var split = innerDocContent.split(" "), obj = {}; var y = document.createElement("p"); for (var x = 0; x < split.length; x++) { if(obj[split[x]] == undefined) { obj[split[x]] = 1; } else { obj[split[x]]++; } } console.dir(obj); for (var i= 0; i < obj.length; i++) { var toadd = document.createTextNode(obj[i]); y.appendChild(toadd); } var x = document.getElementById(tagID); x.appendChild(y); }
Spliting String and getting appropriate value in JavaScript
I have a string where |||| means next to it is the directory. ||| means the user is allowed to access this directory and || means the files allocated to these users follow. I need to find allocated file names of a specific user from this string. I have tried to split the string and assign values to an array but I am not able to get the result I'm looking for. This is the string: ||||Root|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,||||1400842226669|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,||||1401191909489|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,LimitTest_20140528164643.xlsx, And here is my attempt: function getData() { var user = 'km11285c'; var value = "||||Root|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,||||1400842226669|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,||||1401191909489|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,LimitTest_20140528164643.xlsx,"; var users = null; var files = null; var Dir = value.split("||||"); var arrayLength = Dir.length; for (var i = 0; i < arrayLength; i++) { users = Dir[i].split("|||"); } return users; } console.log(getData()); and the jsFiddle
I changed your jsfiddle example a bit so maybe you need to change the code here and there, but something like this should work: function buildTree(data) { var tree = []; var dirs = data.split("||||"); // Remove the first entry in the array, since it should be empty. dirs.splice(0, 1); for (var i = 0; i < dirs.length; ++i) { var tempArray = dirs[i].split("|||"); var dirName = tempArray[0]; var usersAndFiles = tempArray[1]; tempArray = usersAndFiles.split("||"); var users = tempArray[0]; var files = tempArray[1]; var treeDir = { name: dirName }; treeDir.users = users.split(","); treeDir.files = files.split(","); tree.push(treeDir); } return tree; } function getData() { var user = 'km11285c'; var value="||||Root|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,||||1400842226669|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,||||1401191909489|||adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,km11285c,km61052,km61639c,adil001,kl04707c,km47389,km58184,km61052,kq61023c,||LimitTest_20140528164643.xlsx,testTask2_20140528140033.xlsx,testTask1_20140528135944.xlsx,testTask2_20140528140033.xlsx,LimitTest_20140528164643.xlsx,"; var tree = buildTree(value); for (var i = 0; i < tree.length; ++i) { var dir = tree[i]; if (dir.users.indexOf(user) >= 0) { console.log("User '" + user + "' has access to directory '" + dir.name + "', which contains these files: " + dir.files.join(",")); } } } getData();
generate list of variables from a FOR loop
var select = []; for (var i = 0; i < nameslots; i += 1) { select[i] = this.value; } This is an extract of my code. I want to generate a list of variables (select1, select2, etc. depending on the length of nameslots in the for. This doesn't seem to be working. How can I achieve this? If you require the full code I can post it. EDIT: full code for this specific function. //name and time slots function gennametime() { document.getElementById('slots').innerHTML = ''; var namelist = editnamebox.children, slotnameHtml = '', optionlist; nameslots = document.getElementById('setpresentslots').value; for (var f = 0; f < namelist.length; f += 1) { slotnameHtml += '<option>' + namelist[f].children[0].value + '</option>'; }; var select = []; for (var i = 0; i < nameslots; i += 1) { var slotname = document.createElement('select'), slottime = document.createElement('select'), slotlist = document.createElement('li'); slotname.id = 'personname' + i; slottime.id = 'persontime' + i; slottime.className = 'persontime'; slotname.innerHTML = slotnameHtml; slottime.innerHTML = '<optgroup><option value="1">00:01</option><option value="2">00:02</option><option value="3">00:03</option><option value="4">00:04</option><option value="5">00:05</option><option value="6">00:06</option><option value="7">00:07</option><option value="8">00:08</option><option value="9">00:09</option><option value="10">00:10</option><option value="15">00:15</option><option value="20">00:20</option><option value="25">00:25</option><option value="30">00:30</option><option value="35">00:35</option><option value="40">00:40</option><option value="45">00:45</option><option value="50">00:50</option><option value="55">00:55</option><option value="60">1:00</option><option value="75">1:15</option><option value="90">1:30</option><option value="105">1:45</option><option value="120">2:00</option></optgroup>'; slotlist.appendChild(slotname); slotlist.appendChild(slottime); document.getElementById('slots').appendChild(slotlist); (function (slottime) { slottime.addEventListener("change", function () { select[i] = this.value; }); })(slottime); } }
You'll have to close in the iterator as well in that IIFE (function (slottime, j) { slottime.addEventListener("change", function () { select[j] = this.value; }); })(slottime, i); and it's only updated when the element actually change
The cool thing about JavaScript arrays is that you can add things to them after the fact. var select = []; for(var i = 0; i < nameSlots; i++) { var newValue = this.value; // Push appends the new value to the end of the array. select.push(newValue); }
How to add dynamically HTML element using JavaScript and Firefox Add-on SDK
I am developing a simple add-on with Firefox SDK that allows the user to review the stored data. I would like to show the items in a panel with a table. The table is created dynamically with the data incoming from the data-base. Firstly, In the JavaScript code I create the items with their properties, then I create a table with the properties as row and I append the items with the value of each properties. I have some problems: the table has only the item's name as first row and the properties name as first column without the value in the cells. It seems that the code is not executed in order, but it seems that the faster functions are executed first. This is the code: self.on('message', function() { var container = $('#container'); container.empty(); items = []; self.port.emit('getItem'); }); self.port.on('items', function(response) { var ok = 0; var items = []; var table = $('#tableContainer #table').clone(); var container = $('#container'); table.append("<tr id=first><th> </th></tr>"); var json = eval('('+ response + ')'); for (var i = 0, len = json.length; i < len; ++i) { var item = { id: json[i], properties: [] }; items.push(item); } items.forEach ( function(item) { table.find('#first').append("<th id="+item.id+">"+item.id+"</th>"); self.port.emit('detailsItem', item.id); self.port.on('details'+item.id, function(response) { var details = eval('('+ response + ')'); var description = []; ndet = details.length; var len = details.length; for (var i = 0; i < len;) { var detail = { dimension : details[i].dimension, value : details[i].value } description.push(detail); ++i; } item.properties = description; }); }); self.port.emit('getDimension'); self.port.on('dimension', function(response) { var dimensions = eval('('+ response + ')'); for(var cont = 0, l = dimensions.length; cont < l; ++cont) { table.append("<tr id=dimension"+cont+"><td>"+dimensions[cont]+"</td></tr>"); items.forEach( function(item) { var prop = []; prop = item.properties; var lun = prop.length; for( var p =0;p < lun;) { if(item.properties[p].dimension == dimensions[cont]) { table.find('#dimension'+cont).append("<td>"+item.properties[p].value+"</td>"); } else { table.find('#dimension'+cont).append("<td> --- </td>"); } ++p; } }); }; }); container.append(table); });