get all links in chrome extension

Multi tool use
get all links in chrome extension
I'm trying to make an extension that collecting social networks links from the web page where user is. So when user clicking button getLinks
we get all links and then by checking condition passing them in the blocks of the extension. I tried to use chrome.tabs.executeScript
, and get links through urls = $$('a');
but it's not working
getLinks
chrome.tabs.executeScript
urls = $$('a');
$('#getLinks').click(function(e)
{
var allLinks = ;
var i = 0;
chrome.tabs.executeScript( null, {"code": "urls = $$('a'); for (url in urls)
{ allLinks[i]=urls[url].href; i++;}"}, function() {
var vk;
var facebook;
var linkedin;
for (var i=0; i<allLinks.length; i++)
{
var profil = (allLinks[i].href);
if(profil.indexOf('vk.com')!=-1)
{
vk = profil;
$('#vk').text(vk);
}
if(profilito.indexOf('facebook.com')!=-1)
{
facebook = profil;
$('#fb').text(facebook);
}
if(profilito.indexOf('linkedin.com')!=-1)
{
linkedin = profil;
$('#linkin').text(linkedin);
}
}
});
});
in urls I'm getting null instead of the content that I'm getting in the console if I'm just will type urls = $$('a');
– Nikita Lvov
Jul 1 at 23:59
2 Answers
2
That's not how executeScript
is used. That code can not access the variables allLinks
and i
because it is executed elsewhere. But you can make use of the returned value of that code like in this other SO question:
executeScript
allLinks
i
$('#getLinks').click(function(e) {
chrome.tabs.executeScript( null, {"code": "var urls = document.querySelectorAll('a'); for(var i = 0; i < urls.length; i++) { urls[i] = urls[i].href; }; urls"}, function(results) {
var allLinks = results[0];
// use allLinks here
});
});
I'm getting null in results and in allLinks variable
– Nikita Lvov
Jul 2 at 1:02
I tried just to declare var x = 10; x in executeScript and it worked and transferred the value of 10 to results. But here it with urls it only gives me null value
– Nikita Lvov
Jul 2 at 1:21
@NikitaLvov Maybe the
$$
is causing that. AFAIK it is only available in dev tools. Try an alternative like querySelectorAll
. Check my edit!– ibrahim mahrir
Jul 2 at 1:26
$$
querySelectorAll
I tried yours with
querySelectorAll
and I tried to use document.links
but it gives me now just empty object– Nikita Lvov
Jul 2 at 1:42
querySelectorAll
document.links
@NikitaLvov Try injecting the code after document end using the
runAt
attribute.– ibrahim mahrir
Jul 2 at 1:49
runAt
So finally I got an answer on my own question and posting here the solution
$('#getUser').click(function(e) {
chrome.tabs.executeScript(null,{code: 'Array.from(document.getElementsByTagName("a")).map(a => a.innerHTML)'},function (results){
var vk = ;
var facebook = ;
var linkedin = ;
var allElements = results[0];
for (var i=0; i<allElements.length; i++)
{
if (allElements[i].indexOf("https://vk.com") !== -1)
{
vk.push (allElements[i]);
}
if (allElements[i].indexOf("https://facebook.com") !== -1 )
{
facebook.push (allElements[i]);
}
if (allElements[i].indexOf("https://www.linkedin.com") !== -1 )
{
linkedin.push (allElements[i]);
}
}
});
All links that we are finding on the page sorted in 3 arrays by belonging to the social networks
Finally! Good job! BTW shouldn't it be
map(a => a.href)
?– ibrahim mahrir
yesterday
map(a => a.href)
yup, I just misprinted and inserted piece from old code :)
– Nikita Lvov
yesterday
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.
When you say it's not working, what behaviour are you seeing?
– craigcaulfield
Jul 1 at 23:18