Add an event listener in your content script that checks for user clicks, capture the target element, and then use chrome.runtime.sendMessage to send the data to your background page or other target destination.
content.js
document.addEventListener('click', function(e){
var target = e.target || e.srcElement;
var attributes = Array.prototype.slice.call(target.attributes).map(function(i) {
return [String(i.name)+": "+String(i.value)]
})
chrome.runtime.sendMessage({method:"captureElement",data:attributes});
},true)
background.js
chrome.tabs.onActivated.addListener(function(tabId, changeInfo, tab) {
// alert(changeInfo.url);
chrome.tabs.executeScript(null, {"file": "content.js"});
});
var container = []
chrome.runtime.onMessage.addListener(function(message){
if(message.method == "captureElement"){
container.push(message.data)
alert(message.data)
}
})
manifest.json
{
"name": "stack practice",
"version": "1.0",
"description": " content script and background page communication",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["content.js"]
}],
"permissions": [
"http://*/",
"contextMenus",
"activeTab"
],
"manifest_version": 2
}