Chrome creates duplicate slave windows on refresh - google-chrome-extension

I'm migrating my chrome extension from manifest v2 to v3 and I faced a problem which was:
When I create a slave window of my webpage, it works great, but if refresh the page, chrome creates the same slave window (as it's supposed) and a duplicate of a slave window.
before migration I handled it with setSelfAsOpener flag in window.create, but now for some reason Im getting an error, which says: Unchecked runtime.lastError: Cannot specify setSelfAsOpener Service Worker extension.
any suggestions how to fix it ?
chrome.windows.create({
type: 'popup',
incognito: true,
url: 'some url',
setSelfAsOpener: true
}, (wnd) => {
console.log(wnd) // returns undefined and throws
} // error mentioned above
)
and without flag it duplicates...

Related

Web Bluetooth & Chrome Extension: User cancelled the requestDevice() chooser

I'm getting the following error when trying to open the Web Bluetooth device selector dialog from a Google Chrome extension popup:
DOMException: User cancelled the requestDevice() chooser.
I'm using TypeScript and browserify with ES2020 target, module and lib. Here is the code that runs from my popup.html
document.getElementById("test-button")?.addEventListener("click", async () => {
await navigator.bluetooth
.requestDevice({ acceptAllDevices: true })
.then((device) => {
alert(device.name);
})
.catch((error) => {
console.error(error); // <- Getting the error here!
});
});
I'm assuming there's some Chrome extension magic that tricks the web-bluetooth into thinking that I clicked away from the dialog, but have no idea how to get around this. Any idea?
As discussed in https://bugs.chromium.org/p/chromium/issues/detail?id=994185, Web Bluetooth is NOT supported in Chrome Extensions popup windows.
It is supported however in a Chrome Extension "tab/options" page as it acts as a regular tab.
I haven't tried to reproduce this yet but I believe the issue is that Web Bluetooth doesn't have code to handle creating a permission prompt as a child of an extension popup window. It works for the options page because it is a normal Chrome tab and would also work for a standalone app window.
My suggestion would be to open a Chrome Extension regular page that contains some code to interact with nearby Bluetooth devices.
// popup.js
button.addEventListener("click", function() {
// Open a page that contains Javascript to handle Bluetooth devices.
chrome.tabs.create({ url: "page.html" });
});

Can a browser extension's browser_action script save data to a user-specified file?

My extension has a lot of options and users can save other data, and all of it is in browser.storage.local. I'd like to provide an option to export the data to a JSON file. From a regular page script, I'd use Blob() and saveAs, but saveAs is undefined in the browser_action context I'm using for my settings page which is the natural home for the option.
My extension uses nativeMessaging so I can send the data to my host-based process and do the save from there, but if I prompt the user for where to save it from the host process, that will cause a switch that they probably wouldn't anticipate. I haven't implemented this, so perhaps that would be OK.
My direction question is: Can the JavaScript for a browser extension's browser_action page save a file to the user's device based on the user clicking a button?
I am targeting Chrome and Firefox only and Windows platform only. I'll add support for Mac later after I port the host process.
Did you try downloads.download()? https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/downloads/download For example:
const data = JSON.stringify({ boolean: true }, null, 2);
browser.downloads.download({
url: URL.createObjectURL(new Blob([data], { type: 'application/json' })),
saveAs: true,
filename: 'file.json'
});

Cypress : not able to add Ignore X-Frame headers extension from chrome

Basically, I want to overcome the CORS exceptions in my testing site as it uses a different superdomain on the login page, for that I set chromeWebSecurity to false. But after that, I was getting another issue
Refused to display 'XXXXXXXXX' in a frame because it set 'X-Frame-Options' to 'deny'.
For this one, I added the Chrome extension 'Ignore X frame headers' and it was working perfectly. But in the Azure DevOps CI pipeline, I am getting the same issue and not able to add 'Ignore X frame headers' as I added it to my testing browser from the web store.
To add the extension to the testing browser before it launches, I tried the below solution
https://github.com/cypress-io/cypress/issues/1763
I have extracted the CRX file and added it to the folder Ignore-X-Frame-headers_v1.1 and the below code for plugins/index.js .
module.exports = (on, config) => { on("before:browser:launch", (browser = {}, args) =>
{ if (browser.name === "chrome") {
args.push("--disable-features=CrossSiteDocumentBlockingIfIsolating,CrossSiteDocumentBlockingAlways,IsolateOrigins,site-per-process");
args.push("--load-extension=cypress/extensions/Ignore-X-Frame-headers_v1.1");
return args; } }); };
Also, set the chromewebSecurity to false in cypress.json
"chromeWebSecurity":false,
I am using the following versions,
Cypress:3.8.0
Chrome: Chrome 79
Finally, the issue is resolved. We should provide the absolute path for the extension and it doesn't take the relative path when it loads the extension to the browser hence the browser was not able to find out the exact file for the extension.

How to fix 'Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.'

I have the following error in the Chrome Dev Tools console on every page-load of my Node/Express/React application:
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist
This error makes a reference to localhost/:1. When I hover over this, it shows http://localhost:3000/, the address I'm viewing the app at in the browser.
Anyone have an idea what is going on? Most of the other threads I've found that bring up this error seem to be related to someone trying to develop a Chrome Extension, and even then they tend to have very few responses.
I'd been getting the exact same error (except my app has no back-end and React front-end), and I discovered that it wasn't coming from my app, it was actually coming from the "Video Speed Controller" Chrome extension. If you aren't using that extension, try disabling all of your extensions and then turning them back on one-by-one?
I found the same problem when developing the Chrome extensions. I finally found the key problem.
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist
The key problem is that when background.js sends a message to the active tab via chrome.tabs.sendMessage, the content.js on the page is not ready or not reloaded. When debugging. We must ensure that content.js is active. And it cannot be a page without refreshing , The old pages don not update you js itself
Here is my code:
//background.js
chrome.tabs.query({active: true, currentWindow: true},function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
console.log(response);
});
});
//content.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
console.log(request, sender, sendResponse);
sendResponse('我收到你的消息了:'+JSON.stringify("request"));
});
The error is often caused by a chrome extension.
Try disabling all your extensions, the problem should disapear.
Solution
For Chrome:
You have the window open with the console error, open up a second new window.
In the second window, go to:
chrome://extensions
Disable each extension by toggling (the blue slider on the bottom right of each card), and refresh the window with the console after toggling each extension.
Once you don't have the error, remove the extension.
If you are an extension developer see this Chrome Extension message passing: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist
The core of the problem is that chrome API behavior change and you need add a workaround for it.
You need to handle window.chrome.runtime.lastError in the runtime.sendMessage callback. This error just needs to be handled. Below is my code:
window.chrome.runtime.sendMessage(
EXTENSION_ID,
{ message:"---" }, // jsonable message
(result) => {
if (!window.chrome.runtime.lastError) {
// message processing code goes here
} else {
// error handling code goes here
}
}
);
});
🙂simple answer:🙂
if you have no response from another End it will also tell you Receiving end does not exist.
detailed answer:
if you have no answer from another end it will also tell you Receiving end does not exist. so if you have any callBack function which should use response in your .sendMessage part, you should either delete it or handle it if you probably have no response from another side.
so
if i wanted to re-write Simple one-time requests section of Message passing documents of google API i will write it with error handlers for callback functions in message-sending methods like this:
Sending a request from a content script looks like this:
chrome.runtime.sendMessage({greeting: "hello"}, function (response) {
if (!chrome.runtime.lastError) {
// if you have any response
} else {
// if you don't have any response it's ok but you should actually handle
// it and we are doing this when we are examining chrome.runtime.lastError
}
});
Sending a request from the extension to a content script looks very similar, except that you need to specify which tab to send it to. This example demonstrates sending a message to the content script in the selected tab.
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {greeting: "hello"}, function(response) {
if (!chrome.runtime.lastError) {
// if you have any response
} else {
// if you don't have any response it's ok but you should actually handle
// it and we are doing this when we are examining chrome.runtime.lastError
}
});
});
On the receiving end, you need to set up an runtime.onMessage event listener to handle the message. This looks the same from a content script or extension page.
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"});
}
);
Removing 'Udacity Frontend Feedback' chrome extension solved the issue for me.
This was happening in Chrome for me and I discovered it was McAfee WebAdvisor. Once I disabled it, the message went away:
The simple fix is to return true; from within the function that handles chrome.tabs.sendMessage.
It's stated
here.
(this is similar to other answers)
I removed "Video Speed Controller" Chrome Extension and the error was removed.
It worked for me like this. In your case there may be some other extensions too which may cause this error.
It was tab bnundler for me: https://chrome.google.com/webstore/detail/tab-bundler/ooajenhhhbdbcolenhmmkgmkcocfdahd
Disabling the extension fixed the issue.
It was a few extensions for me. Disabling and then re-enabling fixed the problem though. Grammarly was one of them. Hope the errors don't return.
Oddly enough, for myself I simply disabled all my extensions and the error went away.
However, after re-enabling all of the ones I disabled, the error was still gone.
I was testing my extension on edge://extensions/ page/tab. Testing it on another tab solved this issue. I think this may also occur for chrome://extensions/.
This is caused simply by installed chrome extensions, so fix this first disable all chrome extensions then start to enable one after another to detect which extension is cause you can enable the remaining.
in my case removing 'adblocker youtube' extension work for me
For me the error was due to the onelogin chrome extension. Removing it fixed the issue.
Cacher Extension in my case - but yeah disable each and then reload page
This is usually caused by an extension.
If you don't want to disable or remove the extension which causes this error, you can filter out this specific error by typing -/^Unchecked\sruntime\.lastError\:\sCould\snot\sestablish\sconnection\.\sReceiving\send\sdoes\snot\sexist\.$/ in the Filter box in the console:
As far as I've seen, this filter will stay until you remove it manually, you can close and reopen the console, restart Chrome, etc as much as you want and the filter will never be removed automatically.
I get the message only on the first Chrome page =
When Chrome is not running, and I open it - either directly or by double-clicking on a page on my desktop.
(I don't know if this matters, but I have "Continue running background apps when Google Chrome is closed" off.)
So, I'm assuming it's Chrome's crap spying/"enhancing user experience" attempt.
I don't know what it's trying to send, but I'm glad it's not able to! :)
So, second (or any but first) tab has no error.
== No need to disable anything (extensions, etc.).
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
I "achieved" this error after
installing the Debugger for Chrome extension in Visual Studio Code,
debugging my code, and
closing and reopening the non-debugging Chrome window. (I had left Chrome running my app while debugging.)
Solutions:
Stop using the extension.
Refresh the non-debugging Chrome window.
Use a different debugger.
Of course, the console error reappears when I redo steps 2 and 3 above with the Debugger for Chrome. This encourages me to use other debuggers instead. Then I don't mistakenly think my own code produced the console errors!
For me this was caused by :
Iobit Surfing Protection & Ads Removal extension
which comes with Iobit advanced system care software. However, the console might provide you with relevant information on what you need do disable or the cause for that problem.
The likely cause of this error, as per google searches is because that extension which causes the error, might be using the chrome.runtime.sendMessage() and then tries to use the response callback.
Error shown in the console
Hope this information helps. Have a great day!
For chrome extension development, it's an error thrown by chrome.tabs.sendMessage method.
Here is the link of related documentation: https://developer.chrome.com/docs/extensions/reference/tabs/#method-sendMessage. In the link you can find the description for the callback function: If an error occurs while connecting to the specified tab, the callback is called with no arguments and runtime.lastError is set to the error message.
According to documentation above, the error is because of the specified tab. I restart chrome and reloaded the extension, and this issue is fixed
I solved this in following way. This is mainly chrome extension problem, try to first empty cache and hard reload for this inspect any window and hold reload button about 2 second then it will appear 3 options click last one which is mentioned above then go to manage extensions and turn off one by one and then turn on again one by one and check it's gone or not.
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist
chrome.runtime.lastError
It's an error that appear in the Chrome devTools console.
And it's related to Chrome Chrome Message Passing
Because of the 'rigid structure' of the Chrome platform, it's not easy to pass data between different Chrome windows/tabs, as it's common to do in the 'object oriented' programming languages.
In Chrome extensions, usually, there is not a single file of code, that runs for all pages or tabs.
It's a common use, to build many different files of code in javascript, for different Windows/tabs/url address.
So, when the software, need to pass data from different windows/tabs/url, it's made using 'message passing'
The 'message passing', allow to send a 'message' and wait to receive a message as 'callBack'
But sometimes happens, in example, that the url of a window, or a tab, change, when there is a 'message passing' function which has been triggered, but still not returned.
If this situation it's not managed by code (as wrote TechDogLover OR kiaNasirzadeh), or it's an old extension not updated, this error can arise.
So as others users said, it could be an extension, which is running in your Chrome.
But if your window doesn't crash, it doesn't have much sense disable an extension because of such message appear in the devTools Console.
And it doesn't have sense try to understand how to fix it (if you are not the developer of the software which is causing the error), because the variables are so many, that it's not said that the error will be triggered again, in the same situation.
In those cases, in my opinion, if you really want to do something, a good thing to do could be, once you find the extension that cause the error, contact the extension developer and report it.
And if you are the developer, you could check for a missing
if(!chrome.runtime.lastError){
// here if there is no error
} else {
// here if there is an error
}
Or, also you could check your code, if you did more than one callBack, e.g. as explained there
wOxxOm comment
I also encountered this problem when I was developing chrome extensions.
Error: Could not establish connection. Receiving end does not exist.
The error message from chrome.tabs.sendMessage
The solution is to make it return the promise and use catch to handle the error and define it yourself. Example
Apparently an error occurred during browser startup and the browser did not launch extensions such as stylish, adblock. I closed the browser and reopened it and my page worked without errors.

gulp refresh active tab after code change in chrome extension

I'm developing a chrome extension and I want to automatically refresh the active tab after a code change
I got gulp-open to work so I don't need to navigate to chrome://extensions anymore and manually click "reload"
now only the active tab refresh remains
I've tried gulp-livereload but couldn't get it to work for developing a chrome extension
any ideas how to approach this?
Could you post the code of your chrome extension? Without much info, I hope this is helpful. You can inject the code below to refresh the page after the chrome extension is reloaded:
chrome.tabs.query({
active: true, // Select active tabs
lastFocusedWindow: true // In the current window
}, function(tabs) {
// Since there can only be one active tab in one active window,
// the array has only one element
var tab = tabs[0];
// Javascript to reload the page
var code = 'window.location.reload();';
// Execute the code for the current tab
chrome.tabs.executeScript(tab.id, {code: code});
});

Resources