pull all info inside li by searching for a specific value inside it
pull all info inside li by searching for a specific value inside it
hello I am working a chrome extension that controls a website and there are these lists:
It looks like this:
<ul class="styles">
<li></li>
<li></li>
<li></li>
etc..
each li contains custom data
I want to be able to pull all the data inside the one I want by seaching for its
data-style-name= "Orange"
href="/example"
I want to be able to search for which li's data--style-name= "Orange"
then pull a specific result from it like the href=
so something like this:
const searchStr = "orange";
const lowerSearchStr = searchStr.toLowerCase();
const foundItem = (ul .styles all the lis??).find(
( data-name ) => data-name.toLowerCase().includes(lowerSearchStr)
);
something like that would work I think.
I want to find the data-name maybe with a keyword as well as the full name
but then pull the specific href thats inside the found li.
Thank You for taking the time to read this and I would really appreciate it if you could help me out ;) <3
li
li
href
querySelector
The code on screenshot simply stores the attribute values in a deduplicated array. As for the problem, it's likely the site constructs the page slightly after the extension's content script runs. To fight this an extension must poll the page periodically using setTimeout/setInterval or use MutationObserver or other events, see Is there a JavaScript/jQuery DOM change listener?
– wOxxOm
Sep 9 '18 at 4:27
the lis in the url you given doesnt have any data attribute, they have a class and style
– Chris Li
Sep 9 '18 at 4:27
wdym by poll the page periodically? @wOxxOm
– user9510595
Sep 9 '18 at 4:29
Make function wait until element exists
– wOxxOm
Sep 9 '18 at 5:50
2 Answers
2
If you want it to be purely in javascript and below code is same as you have in your screenshot,
search_str = "orange";
search_attr_name = "data-style-name";
var all_styles = my_document.querySelectorAll("[data-style-name]");
matching_url = "";
if(all_styles.length)
all_styles.forEach(function(e)
var data_style_name = e.attributes[search_attr_name].value;
console.log("TEST data_style_name: ", data_style_name);
if(data_style_name.length && data_style_name.toLowerCase().indexOf(search_str) > -1)
if(typeof e.attributes["href"].value !== "undefined")
matching_url = e.attributes["href"].value
);
console.log("matching_url: " + matching_url);
Its not working for some reason can I maybe email you my entire js file so you can see why its not working? my email is wojprod@gmail.com
– user9510595
Sep 9 '18 at 4:57
can you share teamviewer id & pass using other means like whatsapp, gmail etc. I know this is the solution & I wants to show it on your computer.
– bhagwanparge
Sep 9 '18 at 5:01
I don't know what teamviewer is? could I just email you my code?
– user9510595
Sep 9 '18 at 5:05
when I run that entire code in the console I just get undefined as a response
– user9510595
Sep 9 '18 at 5:08
you can download it from teamviewer.com download the appropriate version you require & I will let you connect to my laptop for demo.
– bhagwanparge
Sep 9 '18 at 5:10
You will need to process each li, as like this,
$("li").each(function()
allDatas = $(this).data();
search_str = "left";
$.each(allDatas, function(key, val)
if(val.length && val.indexOf(search_str) > -1)
console.log(val);
);
);
it doesnt work?? nothing pops up in the console
– user9510595
Sep 9 '18 at 4:14
are you searching in the style attribute of the li tag?
– bhagwanparge
Sep 9 '18 at 4:18
i dont think I can use jquery in my background.js script for some reason
– user9510595
Sep 9 '18 at 4:19
Uncaught ReferenceError: $ is not defined at XMLHttpRequest.xhr.onreadystatechange
– user9510595
Sep 9 '18 at 4:20
if you can't use jquery then it is not an issue we can convert it into javascript but what is your aim on the given webpage?
– bhagwanparge
Sep 9 '18 at 4:20
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you post the actual HTML (or mocked-up HTML) rather than an empty list? Also, if you're searching
li
s, why would anli
have anhref
attribute? Sounds like you're just looking forquerySelector
– CertainPerformance
Sep 9 '18 at 1:53