I am first time using chrome extension for web crawling. I get data in HTML popup and then submits it to PHP script on button click. Now I want to do this functionality on page
Use the background page
In your manifest, change your current "background"
key with:
"background": { "page" : "background.html", "persistent": true}
and your "content_scripts"
key with:
"content_scripts": [{"matches":"the_URLs_you_want_to_scrape", "js" : ["content.js"], "run_at": "document_idle"}]
This will be background.html:
<html>
<body>
<form id=myForm>
<input type="text" name="name" id="name">
<input type="text" name="address" id="address">
<input type="text" name="reg_date" id="reg_date">
<input type="text" name="legal_form" id="legal_form">
</form>
<script src="background.js"></script>
</body>
</html>
In your background.js file include this:
chrome.runtime.onMessage.addListener(function(message){
if (message.from == "cs") {
document.getElementById("name").value = message.name;
document.getElementById("address").value = message.address;
document.getElementById("reg_date").value = message.reg_date;
document.getElementById("legal_form").value = message.legal_form;
var encodedData = [].reduce.call(document.getElementById("myForm").elements, function(acc,cur){
acc.push(cur.name+"="+encodeURIComponent(cur.value));return acc},[]).join("&");
var xhr = new XMLHttpRequest();
xhr.onload = function(){
//check the response
}
xhr.open("POST",your_link_to_PHP_script);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.send(encodedData);
}
});
Then your content.js will directly be:
var myMessage = {
from: "cs",
name : document.querySelector('ul.companyDetails li:nth-child(1) > br').nextSibling.nodeValue.trim(),
address : document.querySelector('ul.companyDetails li:nth-child(2) > br').nextSibling.nodeValue.trim() + " "
+ document.querySelectorAll('ul.companyDetails li:nth-child(2) > br')[1].nextSibling.nodeValue.trim(),
reg_date : document.querySelector('ul.companyDetails li:nth-child(3) > br').nextSibling.nodeValue.trim(),
legal_form : document.querySelector('ul.companyDetails li:nth-child(4) > br').nextSibling.nodeValue.trim()
};
chrome.runtime.sendMessage(myMessage);
Bonus: you don't need JQuery :)