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?
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.
Related
I'm having troubles getting chrome storage API to work in MV3. I've tried this so far and even in the service worker console it returns an undefined result
chrome.storage.local.set({ 'test': 'test' }, function() {
chrome.storage.local.get('test', function(result) {
console.log('Value currently is ' + result);
});
});
Here is my manifest I'm not sure if that's an issue
{
"manifest_version": 3,
"name": "NAME",
"description": "Description",
"version": "0.0.3",
"background": {
"service_worker": "background.js"
},
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"action": {
"default_icon": {
"16": "favicon.ico"
},
"default_title": "Outboundly",
"default_popup": "index.html"
},
"permissions": [
"storage",
"scripting",
"activeTab",
"tabs",
"unlimitedStorage"
],
"host_permissions": ["<all_urls>"]
}
I've also tried it in the content script and within the popup context as well.
Should you call in ['test']?
chrome.storage.local.set({key: value}, function() {
console.log('Value is set to ' + value);
});
chrome.storage.local.get(['key'], function(result) {
console.log('Value currently is ' + result.key);
});
reference
I'm not sure why but I changed the position of the permissions around in the manifest, and that seemed to have fixed things. Possibly a bug.
I have connected web page with the chrome extension. Collecting response in background.js. Following is my code:
manifest.json
{
"manifest_version": 2,
"name": "CIC Wallet",
"description": "The CIC Wallet in your browser",
"version": "1.0",
"browser_action": {
"default_icon": "Image/CI_logo-01.png",
"default_popup": "popup.html"
},
"icons": { "16": "Image/CI_logo-01.png",
"48": "Image/CI_logo-01.png",
"128": "Image/CI_logo-01.png"
},
"background":{
"scripts": ["background.js"]
},
"externally_connectable": {
"matches": ["http://127.0.0.1:5500/main.html"]
},
"web_accessible_resources": [
"popup.js", "background.js"
],
"permissions": [
"activeTab"
]
}
background.js
chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse) {
console.log('from webpage');
window.open("popup.html", "CIC Notification", "width=357,height=600,status=no,scrollbars=yes,resizable=no");
document.getElementById("sendtrform").style.display="block";
});
main.js
window.addEventListener('load', function load(event){
var createButton = document.getElementById('btn_buy_trust');
createButton.addEventListener('click', function() {
var laserExtensionId = "myextID";
chrome.runtime.sendMessage(laserExtensionId, 'Hi from Webpage',
function(response) {
});
});
});
main.js is the js file from my webpage, on button click, I am sending the message. In background.js, I am collecting the response and opens my extension HTML file in the separate window. Here in background.js, I am not able to access HTML elements from the popup.html file.
I am getting an error for this line:
document.getElementById("sendtrform").style.display="block";
Can somebody help me, how can I access HTML elements from background.js?
I'm developing chrome extension that need to send messages to Content Script and everything works until I replace default popup by separated window. After that communication on messages doesn't work.
My manifest:
{
"manifest_version": 2,
"name": "Overlight Live Reload",
"description": "Overlight Testing Tool",
"version": "0.0.1",
"content_security_policy": "script-src 'self' http://localhost:3000/; object-src 'self'",
"permissions": ["activeTab", "tabs", "storage", "declarativeContent", "<all_urls>"],
"browser_action": {
"default_title": "Overlight"
},
"background": {
"scripts": ["background/background.js"],
"persistent": false
},
"icons": {
"16": "images/get_started16.png",
"32": "images/get_started32.png",
"48": "images/get_started48.png",
"128": "images/get_started128.png"
},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"css": [],
"js": ["contentScripts/lodash.js", "contentScripts/listeners.js"]
}
]
}
Listener (content script):
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello")
sendResponse({farewell: "goodbye"});
});
Execution from extension app:
export const testSendToPage = () => {
console.log('Sending message to web page')
window.chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
window.chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
}
backgroung scipt that creates extension window:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.windows.create({
height: 400,
width:400,
url: chrome.runtime.getURL("../popup.html"),
type: "popup"
});
});
Everything works fine, when instead creating separated window I'm using default popup. What's more when I try use window.chrome.tabs.executeScript from separeted window exception I've got this.
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-extension://ciipdholchghlckepfaecipbjleanmbg/popup.html". Extension manifest must request permission to access this host.
Messages from content script to extesion app works great. How can I communicate from extension App to content script from extension separated window?
I want send message from webpage to chrome extension, but it not working. I see background function not receiving message send from content.js
Here:
manifes.json:
{
"name": "My Checker",
"version": "1.0",
"description": "http://www.abc.dev Extension!",
"manifest_version": 2,
"permissions": [
"*://*.google.com/",
],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["*://*.abc.dev/*"],
"js": ["jquery-3.3.1.min.js", "content.js"],
"run_at": "document_end"
}
],
"externally_connectable": {
"matches": ["*://*.abc.dev/*"]
},
"browser_action": {
"default_popup": "popup.html"
}
}
And background.js:
console.log("background page in loading...");
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
console.log("listener for request");
console.log(request);
if (request.openUrlInEditor)
console.log("Open :" + request.openUrlInEditor);
sendResponse({success : "success"});
}
);
And content.js:
chrome.runtime.sendMessage("acldjllcapdfejdafbkjnfmpahdkendo", {openUrlInEditor: "https://www.google.com"}, function(response) {
if (response && !response.success)
handleError(url);
}
);
Cross-extension messaging.
you can use the messaging API to communicate with other extensions. This lets you expose a public API that other extensions can take advantage of.
after reading the tutorials I still can't make my extension to work after clicking on it, it starts working as soon as the page loaded.
Here is my manifest.
{
"manifest_version": 2,
"name": "My extension",
"description": "This extension bla bla.",
"version": "3.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["make_changes.js"]
}
],
"background": {
"scripts": ["make_changes.js"],
"persistent": false
}
}
and here is my make_changes.js:
var oldSource = document.documentElement.innerHTML;
document.body.innerHTML = Make_change(oldSource);
function Make_change(source){
...
}
I've also got the background.HTML file, but it works without it. It seems that it is not correct:
chrome.tabs.executeScript(null, {file: "make_changes.js"});
You need to add a listener for the browser action onClicked event:
http://developer.chrome.com/extensions/browserAction.html#event-onClicked
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {file: "make_changes.js"});
});