I am making an Chrome extension and it needs to get some values from a webpage. The page has a script element with a function:
<script>D.setConst({"TIME":1444940082,"LANG":"En","ID":1463887,"DEV":false,"DEV_NAME":......
When I type in my browser console D.TIME, I get the time value. In that function there are many variables and I need few of them for my extension.
Steps which I have done: 1. manifest file has all the permissions, declared the content script. 2. have a popup.html window with a button which executes code from my content script:
function click(e) {
chrome.tabs.executeScript(null,
{code:'var btn = document.createElement("SCRIPT"); btn.innerHTML = "console.log(D.nonce.name);"; document.body.appendChild(btn);'},function(results){ console.log(results); } );
//window.close();
}
document.addEventListener('DOMContentLoaded', function () {
var divs = document.getElementById('message');
divs.addEventListener('click', click);
});
As a result in my extension console I get a result [object], but no the value I need. I get the result in the browser console, but I can't get it for the extension.
I have tried to find a solution, but all I have found is how to inject a script in the DOM.
Please help.
Edited.
manifest file
{
"name": "tesiting",
"description": "generic",
"version": "1.0",
"permissions": [
"https://www.example.lv/*",
"https://*.example.*",
"activeTab"
],
"content_scripts": [
{
"matches": ["https://www.example.lv/*"],
"js": ["testevent.js"]
}
],
"browser_action": {
"default_icon": "icon.png",
"default_title": "Mhmmm",
"default_popup": "popup.html"
},
"manifest_version": 2
}
popup.html
<!doctype html>
<html>
<head>
</head>
<body>
<div id='message'>Geting code</div>
<script type="text/javascript" src="testevent.js"></script>
</body>
</html>
content script testevent.js
window.addEventListener("MyCustomEvent", function (e) {
var check = e.detail.passback;
console.log(check);
});
function click(b) {
chrome.tabs.executeScript(null,
{file: "eventer.js"});
}
document.addEventListener('DOMContentLoaded', function () {
var divs = document.getElementById('message');
divs.addEventListener('click', click);
});
eventer.js
var scr = document.createElement('script');
scr.textContent = '(' + function () {
var check = [console.log(D.nonce.name)]; //this is the function which I execute to get the desired values from it, right now it should get a Name.
var event = document.createEvent("CustomEvent");
event.initCustomEvent("MyCustomEvent", true, true, {"passback":check});
window.dispatchEvent(event); } + ')();';
(document.head || document.documentElement).appendChild(scr);
scr.parentNode.removeChild(scr);