In Chrome it is possible to run content scripts after an extension is installed, that is, the code below works and the words 'content script' are shown in the console log of all open tabs after the installation.
In Edge, however, this does not work. It seems to be a bug in Edge.
Any ideas how to work around so that content-script.js is executed after extension installation in all tabs?
background.js
browser.runtime.onInstalled.addListener(function(details) {
var scripts = browser.runtime.getManifest().content_scripts[0].js;
browser.tabs.query({}, function(tabs) {
tabs.forEach(function(tab){
for(var k=0; k<scripts.length; k++) {
browser.tabs.executeScript(tab.id, {
file: scripts[k]
});
}
});
});
});
content-script.js
console.log("content script");
I can see that you are testing the extension with the MS Edge legacy version.
Microsoft is no longer accepting the new extensions for the MS Edge legacy version.
I suggest you develop an extension for the new MS Edge Chromium browser. You can encourage your users to move to the new Edge Chromium browser. As the new MS Edge uses the Chromium browser engine, it may also help to fix this issue.
Related
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" });
});
I want to attach my extension(.xpi file) to the Firefox using selenium.Post that I want to launch a url in the same. However I cannot attach the extension to the firefox.
I tried looking up this issue on the web.I found issues related to add-on signing and something related to versions of firefox (i.e adding extensions is not supported in newer versions of firefox).I went through the known issues on github for GeckoDriver and didn't find much.
Firefox version:- 70.0 (64-bit)
"geckodriver": "^1.19.1",
"selenium-webdriver": "^4.0.0-alpha.3",
require('geckodriver')
let webdriver = require('selenium-webdriver');
let firefox = require('selenium-webdriver/firefox')
let firefoxOptions = new firefox.Options().addExtensions(`${__dirname}/../../../packages/firefox/extension-dev#pixm.net-2.0.0.0-firefox.xpi`)
describe(firefoxBasic[i].name, function () {
this.timeout(timeOut);
let driver;
before(function () {
mockApi.setAdpFlag(false)
return new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(firefoxOptions)
.build()
.then(d => {
driver = d;
});
});
// Some more code
})
I expect the extension to be attached to the firefox so that i can test the functionality of the extension by running Test cases.
I tried by adding signed extension xpi file and it worked with the same code.
There are ways to sign your extension. Refer below link for more details on how to sign add-ons for firefox.
https://support.mozilla.org/en-US/kb/add-on-signing-in-firefox?as=u&utm_source=inproduct
Is there any way to debug chrome extension using debugger ( break points and step in/out)
beside console.log ?
Chrome 70.x debugging of chrome background scripts is broken, especially when you dynamically load them and they are not in the manifest. Have a ticket open to get it fixed; however they have not been very helpful; however found a work around...
Put a console.log("yourvariablenamehere") in your background.js script.
Hit F12 to open the dev tools, anchored to the bottom of the web page.
Load the background script via a button in your popup.html. Something like this from a button event...
var guid = CreateGuid();
chrome.tabs.executeScript(null, { file: "script/jquery-3.3.1.js" }, function () {
$.get("script/scrollPage.js?ver=" + guid, function (sScriptBody, textStatus, jsXHR) {
chrome.tabs.executeScript(null, { code: sScriptBody });
}, "text");
});
In the dev tools console you should see your logged variable. On the same line as the logged message is a VM with a number tacked onto it, a virtual script page. Select that VM page and then you get to the background script! Now put a breakpoint in the virtual script page, click the same button in your popup.html and it gets hit. And when you reload the popup and execute the background script that breakpoint is hit!
Hope this helps.
If you want to inspecting content scripts, a great method I found is using Console by selecting your extension in javascript context:
By selecting the extension you will have access to the global objects within that extension.
Reference:
Using the Developer tools to debug your extension
It seems like the externally_connectable feature that allows a website to communicate with an extension is still in the dev channel and not yet stable. Are there any other ways to allow a specific website to communicate with my extension, while I wait for this feature to become stable? How have chrome extension developers traditionally done it?
Thanks Rob W for pointing me in the direction of HTML5 messaging. For the benefit of other chrome extension developers, I'm writing about the general problem I was trying to solve and the solution that worked in the end.
I am making a chrome extension that can control music playback on a tab via a popup player. When a user clicks on play/pause/etc on the popup player, the extension should be able to convey that message to the webpage and get back a response stating whether the action was accomplished.
My first approach was to inject a content script into the music player page. The problem is, though, that content scripts operate in a "sandbox" and cannot access native javascript on the page. Therefore, the content script was pretty useless (on its own), because while it could receive commands from the extension, it could not effect any change on the webpage itself.
One thing that worked in my favor was that the website where the music was playing belongs to me, so I could put whatever javascript I wanted there and have it be served from the server. That's exactly what I used to my advantage: I created another javascript file that would reside on the website and communicate with the content script mentioned above, via the window object of the page (i.e. HTML5 messaging). This only works because the content script and the javascript file both exist in the same webpage and can share the window object of the page. Thanks Rob W for pointing me to this capability. Here is an example of how the javascript file on the page can initiate a connection with the content script via the window object:
content_script.js (injected by extension into xyz.com):
window.addEventListener("message", function(event) {
if(event.data.secret_key &&
(event.data.secret_key === "my_secret_key") &&
event.data.source === "page"){
if(event.data.type){
switch(event.data.type) {
case 'init':
console.log("received connection request from page");
window.postMessage({source: "content_script", type: 'init',
secret_key: "my_secret_key"}, "*");
break;
}
}
}
}, false);
onpage.js (resides on server and served along with xyz.com):
window.postMessage({source: "page", type: 'init',
secret_key: "my_secret_key"}, "*");
window.addEventListener("message", function(event) {
if(event.data.secret_key &&
(event.data.secret_key === "my_secret_key") &&
event.data.source === "content_script"){
if(event.data.type){
switch(event.data.type) {
case 'init':
console.log("connection established");
break;
}
}
}
}, false);
I check the secret key just to make sure that the message originates from where I expect it to.
That's it! If anything is unclear, or if you have any questions, feel free to follow up!
You could have an extension inject a content script alongside a web page, and use that to pass messages back and forth between the website and the background page of the extension.
It's tedious, though, and externally connectable is a lot nicer.
Google Wallet seems to get stuck at the loading screen and doesn't show the form
google.load('payments', '1.0', {
packages: ['sandbox_config'],
callback: cbk})
Callback is
goog.payments.inapp.buy({
jwt: getToken,
success: function() {
console.log("success");
},
failure: function() {
console.log("fail");
}
});
Console shows
Uncaught Error: SecurityError: DOM Exception 18 5F00.cache.js:23
com_google_checkout_inapp_client_gwt_init_init
This is on Google Chrome - Version 25.0.1337.0 dev. It works for me on the demo page, but not on my own pages.
EDIT:
Installed Google Chrome Canary - Version 25.0.1347.2 canary
Seems to not be an issue, works perfectly in Canary - is this just a Google Chrome dev glitch or has anyone else seen this?
EDIT 2:
Now it doesn't work in canary either (1348) I have also tested in Chrome stable, where it works.
Actually it doesn't seem to be related to the browser version, as reinstalling Canary and wiping Application Support data files makes it work again
Is there a way to get support via email from Google somehow? I know it's Google and they don't talk to people, but maybe there is some way?
Based off of your experiences it seems like a Chrome dev glitch.
Sidenote: I'm not sure if testing your app in dev/canary releases is the best way to catch potential bugs. Are you also testing in IE/FF/Chrome stable?