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"]
}
]
}
Related
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.
I want to fiddle with chrome tabs, in particular, I want to group them using chrome.tabs.group
https://developer.chrome.com/docs/extensions/reference/tabs/#method-group
However, this function is undefined
Here is my manifest.json
{
"name": "Test",
"version": "1.0",
"description": "Test",
"manifest_version": 2,
"permissions": ["tabs"],
"page_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"],
"persistent": false
},
"commands": {
"toggle-pin": {
"suggested_key": { "default": "Ctrl+Shift+P" },
"description": "Test Action"
}
}
}
This is my background.js script
chrome.commands.onCommand.addListener(function (command) {
if (command == "toggle-pin") {
chrome.tabs.query({ currentWindow: true }, function (tabs) {
// Here i want to use the chrome.tabs.group method, but
// it is not defined
})
}
})
What do I miss?
As #wOxxOm pointed out, this feature is only available in Chrome 88 (which is in beta right now 12 Dec 2020)
I am trying to implement my first PWA service worker and I have referred only google documentation. For now my manifest.json is complete and the service worker is a simple code as below.
// sw.js
var CACHE_NAME = "MyFirstPWA";
var filesToCache = [
// some css and js files to cache
];
self.addEventListener('install', function (e) {
e.waitUntil(caches.open(CACHE_NAME).then(function (cache) {
return cache.addAll(filesToCache);;
}).then(function (e) {
return self.skipWaiting();
}));
});
self.addEventListener('activate', function (event) {
event.waitUntil(caches.keys().then(function (cacheNames) {
return Promise.all(cacheNames.map(function (cacheName) {
return caches.delete(cacheName);
}));
}));
});
self.addEventListener('fetch', function (event) {
event.respondWith(caches.match(event.request).then(function (response) {
return response || fetch(event.request);
}).catch(function (e) {
console.log(e);
}));
});
// Manifest.json
{
"name": "MyFirstPWA",
"short_name": "MyFirstPWA",
"icons": [
{
"src": "/images/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "/images/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "/images/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "/images/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "/images/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "/images/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "/images/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"start_url": "/?utm_source=pwa&utm_medium=pwa",
"orientation": "portrait",
"display": "standalone",
"theme_color": "#0054a6",
"background_color": "#ffffff",
"scope": "/",
"splash_pages": null
}
The working status of google's mini-info bar feature as below.
I want to know if I am missing something or have implemented wrongly?
Please suggest.
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);
}
});
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?