Chrome Extension background.js not receiving message sent from content.js - google-chrome

I want to send data from my inject.js to background.js so that it calls for an external API to store the data into DB.
Here are my sample files
manifest.json
{
"name": "Income Tax Notice Reader",
"description": "Reads Income Tax notices once you login into the IT account",
"version": "1.0",
"manifest_version": 3,
"permissions": [
"alarms",
"notifications",
"activeTab",
"tabs",
"storage",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [{
"exclude_globs": [
"chrome://extensions/"
],
"matches": [
"<all_urls>"
],
"all_frames": true,
"js": ["jquery.min.js","content.js"]
}],
"web_accessible_resources": [{
"resources": ["inject.js","jquery.min.js"],
"matches": [ "<all_urls>" ]
}],
"action": {},
"icons": {
"16": "favicon.ico"
},
"commands":{
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+F",
"mac": "MacCtrl+Shift+F"
}
}
}
}
My content.js file
var script = document.createElement('script');
script.src = chrome.runtime.getURL('jquery.min.js');
(document.head||document.documentElement).appendChild(script);
var script = document.createElement('script');
script.src = chrome.runtime.getURL('inject.js');
(document.head||document.documentElement).appendChild(script);
window.addEventListener("message",function(event){
alert(event.data.message);
chrome.runtime.sendMessage(event.data);
return true;
});
Background.js
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['content.js']
});
}
})
chrome.runtime.onMessage.addListener(function(event){
console.log(event.data.message);
return true;
});
inject.js
window.postMessage({message:"send message to background worker"});
The message is reaching till content.js, but it is not going from content.js to background.js, not sure what I am doing wrong here, tried multiple ways but still failing to understand the issue.

Related

Chrome extension permissions for localhost not working in manifest.js

I am trying to get localhost permission in my manifest.json so I can use chrome.tabs.executeScript but it is not working. I have tried nearly everything you can think of. Here is an example:
{
"manifest_version": 2,
"name": "extName",
"version": "1",
"description": "extDesc",
"content_scripts": [
{
"matches": [
"http://localhost:3000/*",
"http://127.0.0.1/*",
"http://*/*",
"https://*/*"
],
"css": [
"style.css"
],
"js": [
"contentScript.js"
]
}
],
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"activeTab",
"tabs"
]
}
But no matter what I try I keep getting
Error during tabs.executeScript: Cannot access contents of url ....
Extension manifest must request permission to access this host.
Actually, I think it's happening also not on localhost
Here is my background.js:
chrome.runtime.onMessage.addListener(
function(message, callback) {
if (message == "runContentScript"){
chrome.tabs.executeScript(null, {
code: 'console.log(window.angular)'
});
}
});

Trying to disable extension from web page

Here I have taken permission for all the needed thing, if something missing here please let me know.
Manifest.json
{
"manifest_version": 2,
"name": "SearchMood",
"description": "This extension shows Google Web Search and Google Image
Search result.",
"version": "1.4",
"author":"Searchmood",
"browser_action": {
"default_icon": {
"128": "icon-128.png",
"16": "icon-16.png",
"48": "icon-48.png"
},
"default_title": "SearchMood",
"default_popup": "background.html"
},
"chrome_url_overrides" : {
"newtab": "popup.html"
},
"icons": {
"128": "icon-128.png",
"16": "icon-16.png",
"48": "icon-48.png"
},
"background": {
"scripts": [ "background.js","popup.js" ]
},
"permissions": [
"activeTab","management","https://ajax.googleapis.com/"
],
"content_scripts": [
{
"matches": [ "http://search.searchmood.com/*" ],
"js": [ "js/restoremodal.js" ],
"all_frames": true,
"run_at": "document_start"
}
],
"externally_connectable": {
"matches": ["http://*.searchmood.com/*"]
}
}
Script on my web page I have:
<div class="links">
<ul>
<li>Terms&Conditions</li> |
<li>Privacy policy</li>|
<li>Contact US</li>
<li><span id="restoreLink" onclick="adi();">Reset Chrome</span></li>
</ul>
</div>
<script type="text/javascript">
function adi(){
var editorExtensionId = "pokioadkjpcbalcpfidmlebofahebkhb";
// Make a simple request:
chrome.runtime.sendMessage(editorExtensionId, {request: "uninstall"});
}
</script>
And this is the code in my extension to listen To this message I have put this code in popup.js. Please guide me where to put that also.
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (request == "uninstall") {
var id = chrome.app.getDetails().id;
chrome.management.setEnabled(id, false);
}
});
This the script I have written. I have some other codes also but that is also not working.
Yes I got the answer In the content Script File We can write code for taking the element from Website And their we will use SendMessage function Make sure You have taken permission for "Management" in manifest.json And persistent in Menifest.json should be "false".
Then you can use onmessage function and disable your extension Codes for your reference:
**HTML**`<span id="restorelink"> disable chrome extension</span>`
**menifest.json**
{
"manifest_version": 2,
"name": "name",
"description": "describe about you extension",
"version": "1",
"browser_action": {
"default_icon": {
"128": "icon-128.png",
"16": "icon-16.png",
"48": "icon-48.png"
},
"background": {
"scripts": [ "background.js"],
"persistent": false
},
"permissions": [
"management"
],
"content_scripts": [
{
"matches": [ "http://your site url where you want to apply the change, you can use more than one seperating with "," ],
"js": [ "restoremodal.js" ],
"all_frames": true
}
]
}
**restoremodal.js**
document.getElementById("restoreLink").addEventListener("click", adi);
function adi(){
chrome.runtime.sendMessage({ value: "anything"});
}
**Background.js**
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.value=="anything") {
var id = chrome.app.getDetails().id;
chrome.management.setEnabled(id, false);
}
});

Background script chrome.tabs is undefined in chrome extension?

I tried this:
background.js
chrome.browserAction.onClicked.addListener(function (activeTab) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response.farewell);
});
});
});
foo.js:
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
new contentFunction()
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.greeting == "hello") {
sendResponse({ farewell: "goodbye" });
}
});
manifest:
{
"manifest_version": 2,
"name": "foo",
"browser_action": {
},
"background": {
"scripts": ["background.js"]
},
"permissions": [
"activeTab",
"tabs",
"<all_urls>"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["foo.js"]
}
]
}

"Cannot access contents of url" Chrome extension error in background.js

I am trying to invoke a current_script from a background script, but I receive the following error:
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/inspector.html?
I understand I need to send also tab.id in executeScript function, I also tried that but with no luck.
UPDATE: I changed background.js to the following but content_script.js is still not working:
chrome.commands.onCommand.addListener(function(command) {
if (command === "toggle-feature") {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
for(var i = 0; i<tabs.length;i++) {
alert(tabs.length);
chrome.tabs.executeScript(tabs[i].id, {"file": "content_script.js"});
alert("h");
}
});
}
});
Here is all my code:
Manifest.nmf
{ "manifest_version": 2,
"name": "Extractor",
"version": "1",
"description": "Extract from 144",
"icons": {
"16": "logo16.png",
"48": "logo48.png",
"128": "logo128.png"
},
"page_action": {
"default_icon": {
"16": "logo16.png",
"48": "logo48.png",
"128": "logo128.png"
},
"default_title": "Extractor"
},
"background": {
"scripts": [ "background.js" ],
"persistent": true
},
"content_scripts": [ {
"matches": [ "https://www.msn.com/*" ],
"js": [ "content_script.js" ]
} ],
"permissions": [
"tabs",
"https://www.msn.com/*",
"activeTab",
"*://*/*"
],
"commands": {
"toggle-feature": {
"suggested_key": {
"default": "Ctrl+Shift+1",
"windows": "Ctrl+Shift+2"
},
"description": "Extract now"
}
}
}
Background.js
chrome.commands.onCommand.addListener(function(command) {
if (command === "toggle-feature") {
chrome.tabs.executeScript(null, {file: "content_script.js"} );
}
});
content_script.js
alert("success");
As you see I have also tried to add all links combinations in permissions but with no luck!
How can I solve this problem and get current_script working when pressing a hot key in a tab?

background.js is being triggered every refresh

It seems as if my background.js is triggered every refresh [planted an alert, and i see it every refresh, also my context menu is duplicating its inner self].
this is my manifest file:
"manifest_version": 2,
"name": "test",
"description": "test",
"version": "1.0",
"permissions": [
"activeTab",
"storage",
"tabs",
"contextMenus",
"https://*/*",
"http://*/*",
"https://www.google.com/_/chrome/newtab*"
],
"browser_action": {
"default_icon": "raj_robot.png",
"default_title" : "MemoMi"
},
"chrome_url_overrides" : {
"newtab" : "mypage.html"
},
"background": {
"scripts" : ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js" : ["selection.js"],
"run_at": "document_end"
}
],
And this is my background script:
function handle_click() {
alert("hi there!");
}
chrome.contextMenus.create({
title: "menu title",
contexts:["selection"],
onclick: handle_click
});
what am I doing wrong here?
Thanks!
Gura
Ok, I found it. I was actually implicitly triggering background.js by using methods in the content scripts, that should have been in the background.js. Just comment out your code and and see if it improves.

Categories