Reputation: 97
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').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);
}
}
});
});
Upvotes: 1
Views: 2986
Reputation: 1
Extract all URLs of any webpage
Step 1: Run JavaScript code in Google Chrome Developer Tools Open Google Chrome Developer Tools with Cmd + Opt + i (Mac) or F12 (Windows). Click on the Console tab. Copy-paste the following JavaScript code and press Enter.
const results = [
['Url', 'Anchor Text', 'External']
];
var urls = document.getElementsByTagName('a');
for (urlIndex in urls) {
const url = urls[urlIndex]
const externalLink = url.host !== window.location.host
if(url.href && url.href.indexOf('://')!==-1) results.push([url.href, url.text, externalLink]) // url.rel
}
const csvContent = results.map((line)=>{
return line.map((cell)=>{
if(typeof(cell)==='boolean') return cell ? 'TRUE': 'FALSE'
if(!cell) return ''
let value = cell.replace(/[\f\n\v]*\n\s*/g, "\n").replace(/[\t\f ]+/g, ' ');
value = value.replace(/\t/g, ' ').trim();
return `"${value}"`
}).join('\t')
}).join("\n");
console.log(csvContent)
Step 2: Copy-paste exported URLs into a CSV file or spreadsheet tools
you will get
URL - The link URL Anchor Text - The label associated with the link. Called "Anchor Text". External - A boolean (TRUE, FALSE)
https://www.linkedin.com/feed/?trk=guest_homepage-basic_google-one-tap-submit
Upvotes: 0
Reputation: 97
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
Upvotes: 1
Reputation: 31692
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:
$('#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
});
});
Upvotes: 1