Get Text From DOM object. Chrome Extension
Get Text From DOM object. Chrome Extension
I have page https://somepage in internet. Here is part of it's code from chrome code:
..............
<div class="wr">
<div class="auth_username">tesеDom@mail.com</div>
<div class="auth_stars">
<i class="fa fa-star-o fa-fw">
...........................
I have seen many answers and examples, but my code still not working. Seems I am missing something. My manifest:
"manifest_version": 2,
"name": "Bot_copy",
"version": "1.0",
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"icons":
"48": "48x48.png",
"128": "128x128.png"
,
"content_scripts": [
"matches": [ "https://mypage.my" ],
"js": ["helloWorld.js"]
],
"permissions": [
"activeTab",
"alarms",
"clipboardRead",
"clipboardWrite",
"bookmarks",
"contextMenus",
"contentSettings",
"downloads",
"history",
"nativeMessaging",
"browsingData",
"proxy",
"webRequest",
"webRequestBlocking",
"cookies",
"tabs",
"webNavigation",
"storage",
"tabCapture",
"notifications",
"http://*/",
"https://*/",
"<all_urls>",
"unlimitedStorage",
"debugger"
],
"browser_action":
"default_title": "Open"
,
"background":
"scripts": ["background.js"]
content script
window.onload = function()
sendMessage();
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse)
sendResponse(
response: "Message received"
);
);
//Send message to background page
function sendMessage()
//Construct & send message
chrome.runtime.sendMessage(
whattodo:"get_authname"
, function(response)
);
background
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse)
var name = document.getElementsByClassName('auth_username').value;
alert(name);
sendResponse(
response: "Message received"
);
);
I received alert "undefined".
Have no idea. Really strange there are no properly documentation I don't understand object document.
Seems I miss how to use documentc object. I tried also document.getElementsByTagName('auth_username'), but it also not working. Which way is correct?
1 Answer
1
You can not access DOM from background script. You can access DOM from content script only. so try to place following code:
var name = document.getElementsByClassName('auth_username').value;
alert(name);
in contentscript and it works.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.