skip to Main Content

my function will recieve a array object from a external api where i can convert that to JSON
lets say this is how my JSON objects look like

{
        "id": 99,
        "title": null,
        "author": "user",
        "content": "content",
        "url": "https://stackoverflow.comt"
    },

    {
        "id": 100,
        "title": null,
        "author": "user",
        "content": "content",
        "url": "https://www.google.com"
    }

after separating particular key value pair from the above received data this is how it looks

{"url":"https://www.google.com/"}

this process is async call as soon as i click on extension this gets triggered and i can see that in console

now in my template
popup.html

    <td>
                <input type="text" name="url" id="url">
                <input type="hidden" name="content" id="content" value="content" />
            </td>

popup.js

chrome.tabs.query({active: true, currentWindow: true },
function callback(tabs) {
  var currentTab = tabs[0]; // there will be only one in this array
  $("#url").val(currentTab.url);
});

it grabs the current current url from the tab and sends to post.js where backend operations happen
now i want to check the url data and compare it to every url present in the JSON object

I’m new to Jquery/Javascript i tried comparing this value to each and every value to that json but its of no use

2

Answers


  1. When you have the json value of key value pair use the following code to get the url.

    jsonKeyValuePair // assuming this variable has key value pair, having url.
    
    var jsonData = JSON.parse(jsonKeyValuePair)
    console.log(jsonData.url) // this should give you the url value 
    
    // You can now compare
    
    if(jsonData.url === currentTab.url) {
      // Do your calculation.
    }
    

    Hope this helps.

    Login or Signup to reply.
  2. The simplest and cleanest way you can compare is using Filter to match the urls in JSON object.

    let currentTab = tabs[0]; // Url for comparing
    
    //jsonObj contains all the json urls
    let matchedUrl= jsonObj.filter(function(item) {
      return item.url === currentTab.url;
    });
    
    console.log(matchedUrl);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search