Javascript Array.length is incorrrect

I have an array of objects which initially has 4 elements. The array.length is proper when I console.log initially. Later I push one more object to the array. The Item is added to the array but the Array.length is incorrect this time. Please have a look at the screenshot

Does someone have any idea what causes this issue

enter image description here

How to make image array shuffle script work in google chrome extension?

I’m new to chrome extensions and javascript so this may be a pitiful question.
I am trying to make a popup where an image appears and then after a user presses a button, the image changes after each button; all images are stored in an array.
This is what my arrayShuffle.js looks like.

  var imagesArray = ["https://i.pinimg.com/originals/93/95/32/939532994e66f1a626362e17e9daab54.jpg", "https://i.pinimg.com/originals/39/9d/13/399d13fea473ef21532f1cba01130327.jpg", "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTrv2lY60BseySP27SOC4tOzY_qxjxauxkst4W-wOLd87bKkvuk"];
function displayImage(){
    var num = Math.floor(Math.random() * imagesArray.length);
    document.canvas.src = imagesArray[num];
  }

Here is my .html file simplified:

I realize I’m new to both javascript and chrome extensions, but I’m still confused about this not working.

Why is papaparse making incorrect subarrays? Dividing data into [0..99][100] array instead of [0][1]…[100]

My code

parse(files: FileList): void {
        const file: File = files.item(0);
        const reader: FileReader = new FileReader();
        reader.readAsText(file);
        reader.onload = e => {
            const csv = reader.result;
            const parsed = this.papaParse.parse(csv, { header: false });
            this.papaParse.parse(csv, {
                complete: (results, file) => {
                    console.log('Parsed', results, file);
                    this.results = results;
                }
            });


        }

    }

It outputs like this
enter image description here

I was expecting the data array to have subarray with the information not another set of arrays.
How do I fix this?

Why is chrome giving me a TypeError when finding largest number in array?

I have the following code to find the largest number in an array:

function max(input) {
 var maxNum = 0;
 for(var i = 0; i <= input.length; i++) {
     if(maxNum < input[i]) {
         maxNum = input[i];
        }
    }
    return maxNum;
}
max([1, 2, 3, 7]);

When I run it in Google Chrome, Edge, or Firefox I get:

max.js:20 Uncaught TypeError: Cannot read property 'length' of undefined
    at max (max.js:20)
    at :1:1

but when I run it on pythontutor for JavaScript, I get the correct results without any syntax error.

chrome.tabs.query does not find match if url property is an array

I’m building a chrome extension and trying to get an array of tabs using chrome.tabs.query({url: arrayOfUrls}):

chrome.tabs.query({url: this.tabUrlsList}, function(tabs){
            var ids = [];
            if(tabs){   
                //creates an array of intergers (tab ids)

                for(var i = 0; i < tabs.length; i++){
                    ids[i] = tabs[i].id;

                }
                console.log(tabs.length)
                console.log(ids)
                \move stuff around using the tabs' ids

The "if" statement I added after i got the following problem (and it hasn't fixed it): the console.log sometimes displays the correct number, and ids, but in many cases shows only one tab queried. Seems quite odd. I found no references to this question anywhere, except for this: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/9107332/
but its from two years ago almost.
if its a windows thing I'm using windows 7.
help will be much appreciated.

How do I locally save the urls visited by an user, through a chrome extension, using JS?

I want to save the url link of the active tab, through a chrome extension, when an user clicks on the extension icon. The url should be saved in local storage, and should remain stored until the active window isn’t closed. I am trying to save the url in an array tablink

Here’s the manifest.json

{
    "manifest_version": 2,

    "name": "saveLink",
    "version": "1.0",

    "browser_action": {
        "default_icon": "xyz.png",
        "default_popup": "popup.html",
        "default_title": "saveLink"
    },
    "permissions": [
        "activeTab",
        "storage",
        "tabs"
    ]
}

Here’s my popup.js which contains the js code for popup.html

var tablink = [];
function getTabUrl(){
  chrome.tabs.getSelected(null,function(tab) {
  var len = tablink.length;
  if(len == 0){
  tablink[0] = tab.url;
}
else{
  tablink[len] = tab.url;
}
console.log(tablink);
 }
}

document.addEventListener("DOMContentLoaded", function() {
  getTabUrl();
  link.addEventListener('click', function() {
    chrome.tabs.update({url: 'https://www.google.com/'});
  });
});

Currently, the console isn’t printing anything. Also, the extension has a button which redirects to the specified link(google.com), which was working fine before I wrote the code for saving tab links, but now isn’t working. Please specify if anything more needs to be added.

How do I delete one item from an array with chrome.storage.sync.remove?

I’m building a Chrome browser extension which includes a feature that lets users save urls. I used chrome.storage.sync to create this feature and didn’t encounter any issues.

However, I also need to let users delete urls they’ve already saved. I created a text input box where they can enter a single url they want to delete (i.e. “www.url.com”). I want to convert this input into a string and use it to remove a matching string from Chrome storage when the user presses a button.

I thought the chrome.storage documentation specifies that chrome.storage.sync.remove can be used to remove a string from a stored array, but I can’t get it to work. Here’s my code:

function removeUrl() {
    var theUrl = document.getElementById('url-input').value;
    chrome.storage.sync.get(null, function(result, error) {

        // Checks if the array (urlList) exists:
        if ('urlList' in result) {
            var theList = result['urlList'];

            // Checks if the url that will be deleted is in the array:
            if (theList.includes(theUrl)) {
                chrome.storage.sync.remove(theUrl);
                chrome.runtime.reload();
            }
        }
        // Error handling goes here
    });
}

This code doesn’t throw any errors, but it doesn’t remove the url from the array either.

Please note that I only want to delete 1 url at a time, there are no duplicate urls in the array, and I am not using any libraries like jQuery. What am I doing wrong?

Periods/ellipsis and string.replace not replacing them, any suggestions?

Not a duplicate, I’ve already tried the possible solutions at:

How to replace all dots in a string using JavaScript

I’ve having a JS issue with string.replace not replacing periods/ellipsis, maybe I’m just really tired but it’s annoying the **** out of me. I’d appreciate any suggestions. This is in a chrome extension content script if that has any relevance.

I’ve tried variations of the below code and .split, other regex, etc. with no luck. All content of document.body.innerText.toLowerCase().trim().split(/s+/); is from the Google homepage after searching for the keyword “google”.

Relevant code:

var kwArr = document.body.innerText.toLowerCase().trim().split(/s+/);

//remove empties, links, etc here...

//stupid period/ellipsis...
//begin loop(i)
kwArr[i] = $.trim(kwArr[i].replace(/.+$/g, ''));
kwArr[i] = kwArr[i].replace('.', '');
kwArr[i] = kwArr[i].replace("./g", "");
kwArr[i] = kwArr[i].replace('/./g', '');
kwArr[i] = kwArr[i].replace('/u2E7F/g', '');
kwArr[i] = kwArr[i].replace('/u2E7F/g', '');
kwArr[i] = kwArr[i].trim().replace(/.+/g, '');
kwArr[i] = kwArr[i].trim().replace(/.{3}/g, '');
//end loop(i)
//end hacks

Array dump with frequency after running kwArr through replace, etc.:

Stupid...

Data from console.log(JSON.stringify(kwArr)):

https://gist.github.com/PlexXoniC/5084ff5fb69785f60bd043a205923601

Any help would be greatly appreciated, thanks!

Tired of periods/ellipsis and string.replace not replacing them, or maybe I’m just tired, any suggestions? [duplicate]

This question already has an answer here:

I’ve having a JS issue with string.replace not replacing periods/ellipsis, maybe I’m just really tired but it’s annoying the **** out of me. I’d appreciate any suggestions. This is in a chrome extension content script if that has any relevance.

I’ve tried variations of the below code and .split, other regex, etc. with no luck. All content of document.body.innerText.toLowerCase().trim().split(/s+/); is from the Google homepage after searching for the keyword “google”.

Relevant code:

var kwArr = document.body.innerText.toLowerCase().trim().split(/s+/);

//stupid period/ellipsis...
//begin loop(i)
kwArr[i] = $.trim(kwArr[i].replace(/.+$/g, ''));
kwArr[i] = kwArr[i].replace('.', '');
kwArr[i] = kwArr[i].replace("./g", "");
kwArr[i] = kwArr[i].replace('/./g', '');
kwArr[i] = kwArr[i].replace('/u2E7F/g', '');
kwArr[i] = kwArr[i].replace('/u2E7F/g', '');
//end loop(i)
//end hacks

Array dump with frequency after running kwArr through replace, etc.:

Stupid...

Any help would be greatly appreciated, thanks!

Periods/ellipsis and string.replace not replacing them, suggestions? [duplicate]

This question already has an answer here:

EDIT: Not a duplicate, I’ve already tried the possible solutions at:

How to replace all dots in a string using JavaScript

I’ve having a JS issue with string.replace not replacing periods/ellipsis, maybe I’m just really tired but it’s annoying the **** out of me. I’d appreciate any suggestions. This is in a chrome extension content script if that has any relevance.

I’ve tried variations of the below code and .split, other regex, etc. with no luck. All content of document.body.innerText.toLowerCase().trim().split(/s+/); is from the Google homepage after searching for the keyword “google”.

Relevant code:

var kwArr = document.body.innerText.toLowerCase().trim().split(/s+/);

//stupid period/ellipsis...
//begin loop(i)
kwArr[i] = $.trim(kwArr[i].replace(/.+$/g, ''));
kwArr[i] = kwArr[i].replace('.', '');
kwArr[i] = kwArr[i].replace("./g", "");
kwArr[i] = kwArr[i].replace('/./g', '');
kwArr[i] = kwArr[i].replace('/u2E7F/g', '');
kwArr[i] = kwArr[i].replace('/u2E7F/g', '');
//end loop(i)
//end hacks

Array dump with frequency after running kwArr through replace, etc.:

Stupid...

Any help would be greatly appreciated, thanks!

EDIT: Data from console.log(JSON.stringify(kwArr)):

https://gist.github.com/PlexXoniC/5084ff5fb69785f60bd043a205923601