After some hours spent in debug, I've managed how to get some data property from a dynamic generated div on a page. I've used the popup.js file as the content script to run when this page is loaded and I was supposing that this will give me the ability to append the retrived data to the popup.html page using jQuery, but this will not work. I need a way to add some html and the data to popup.html, how I can achieve this? I need to use a separate content script to grab the data and another one script to manage the popup.html page in my extension? Any suggestion?
manifest.json
{
"name": "FBSR",
"short_name": "FBSR",
"version": "1.0",
"author": "Me",
"description": "!",
"manifest_version": 2,
"permissions": ["tabs"],
"browser_action": {
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["https://www.facebook.com/*"],
"js": ["js/jquery.min.js","js/popup.min.js"]
}]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<title>GOAL - v1.0</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="css/fontawesome-all.min.css" type="text/css" />
<link rel="stylesheet" href="css/main.min.css" type="text/css" />
<script src="js/jquery.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<script src="js/popup.min.js" type="text/javascript"></script>
</head>
<body>
<div class="container wrapper">
<!-- I want to append the data in the stories row div -->
<div class="row stories"></div>
<hr>
</div>
</body>
</html>
popup.js
(function($){
$(document).ready(function(){
console.log('Extension Started!');
var el = $(document).find('#stories_tray');
var child = el.find('._827c');
$.each(child, function(i){
var div = $(child[i])
.find('._7h4p')
.attr('data-onkeypress');
var d = JSON.parse(div);
var c = d[0].a;
var l;
// also this part is not clear, in the beginning I was not able to get the desired data attribute property.
//See this question: https://stackoverflow.com/questions/57698205/get-dynamic-div-data-properly-with-jquery
if(typeof c != 'undefined'){
console.log(d[0].a[0].preloadImageURIs[0]);
l = d[0].a[0].preloadImageURIs[0];
}
// these lines will not work, nothing is added to the DOM
var html = '<div class="col-4 card">';
html += '<a class="" href="'+l+'">VIEW</a>';
html += '</div>';
$('.stories').append(html);
});
});
}(jQuery));