background.js is being triggered every refresh - google-chrome

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.

Related

Submit form from content script of a Chrome extension

I am creating an extension that fills a form and submits it. The extension is able to fill the form inputs but gives the following error when triggers the submit event on the form.
Form submission canceled because the form is not connected
I am doing this from a content script using the following code.
var form = $('<Query selector for form>');
form.submit();
Following are the manifest file contents:
{
"name": "My Extenssion",
"description": "My extension description",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": [
"tabs", "background", "*://*/*", "http://*/*", "https://*/*", "storage", "activeTab", "scripting"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [ "jquery-3.6.0.min.js", "aes.js", "content.js" ]
}
],
}

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);
}
});

Is it possible to create a chrome extension showing an overlay div without adding it to the DOM?

I am trying to create a simple chrome extension and at the moment I am injecting my scripts and stylesheets to the DOM to show an overlay div in the browser window. However when I change the tab or address the div won't appear until document is ready.
So is there a way to show an overlay div on all tabs without adding it to the DOM?
My manifest file:
{
"name": "My extension",
"description": "test",
"version": "0.0.1",
"manifest_version": 2,
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": [
"css/content.css",
],
"js": [
"libs/jquery-3.2.1.min.js",
"js/content.js"
]
}
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "My extension"
},
"permissions": [,
"https://www.example.com/*", // I will update the div content from this URL
"storage"
],
"web_accessible_resources": [
"fonts/*.*"
]
}
I am adding it from my content.js like:
$('body').prepend(div);

Chrome extension access webrequest headers

I'm trying to write simple extension that would act on request headers.
In documentation there is something about chrome.WebRequest, but I have no idea how to make it work....
When I put listener to content page, chrome.WebRequest is undefined, when I put it to background section, totally nothing happens...
manifest
{
"manifest_version": 2,
"name": "RequestSpy",
"version": "0.1",
"description": "HTTP/S Request Analizer",
"background": [
{
"scripts": ["scripts.js"]
}
],
"icons":{
"128":"spy.png"
},
"permissions": [
"*://*/*","webRequest"
]
}
script.js
alert('haha');
chrome.webRequest.onHeadersReceived.addListener(function(details){
console.log(details);
alert('huhu');
});
Any help?
manifest.json
{
"name": "OnRequest",
"version": "1.0",
"description": "I can't has cheezburger!",
"permissions": ["webRequest",
"webRequestBlocking",
"http://*/*",
"https://*/*"],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
background.js
chrome.webRequest.onHeadersReceived.addListener(function(e){
alert("onHeadersReceived");},{urls: ["http://*/*", "https://*/*"]}, ["blocking"]
);