chrome.tabs.executeScript shows unknown error - geolocation

This is my code which I use for geolocation.
It does not log the coordinates to the console even though I have console.log. It shows an error saying "chrome/ExtensionProcessBindings:95 Error during tabs.executeScript: Unknown error."
However, when asked to alert; it alerts the right coordinates
background.html
<script>
chrome.tabs.executeScript(null, {file: "content_script.js"});
</script>
content_script.js
navigator.geolocation.getCurrentPosition(function(position) {
console.log(position.coords.latitude+" "+position.coords.longitude);
});
manifest.json
{
"name" : "Geolocation",
"version" : "0.1",
"background_page" : "background.html",
"permissions":["tabs","http://*/*", "https://*/*","*://*/*"]
}

Your code works fine for me.
I think the problem is that you are calling chrome.tabs.executeScript() right in the beginning of a background page, which means it probably tries to inject this script right into chrome://extensions/ when you are enabling your extension.
You need to make sure you are injecting the script to a regular loaded tab. For example I tried injecting it when a user clicks on browser action icon, and it worked:
chrome.browserAction.onClicked.addListener(function() {
chrome.tabs.executeScript(null, {file: "content_script.js"});
});

Related

How do I use showOpenDialog withe Electron’s IPC?

I am learning to live without remote in my electron app. I have got to the point where I need to open a file from the renderer, which I understand required the main process to show the file dialog and send back the results.
In my main.js, I have the following:
ipcMain.on('open-file',(event,data)=>{
dialog.showOpenDialog(null, data, (filePaths) => {
event.sender.send('open-file-paths', filePaths);
});
});
In my render process, which I call pager.js, I have the following:
ipcRenderer.send('open-file',{
title: 'Title',
defaultPath: localStorage.getItem('defaultPath')
});
ipcRenderer.on('open-file-paths',(event,data)=>{
console.log(event);
console.log(data);
});
The file open dialog works well enough, but I don’t know how to get the results. the ipcRenderer.on('open-file-paths',…) doesn’t get called, so that’s obviously not the right way to do it. I would like to get either the selected path(s) or a cancelled message.
How do I get the results for showOpenDialog in the render process?
OK, I think I’ve got it.
Thanks to an answer in ShowOpenDialog not working on recent versions of electron-js, I see that showOpenDialog now returns a promise, which means reworking the code in main.js. Here is a working solution:
// main.js
ipcMain.on('open-file',(event,data)=>{
dialog.showOpenDialog(null, data).then(filePaths => {
event.sender.send('open-file-paths', filePaths);
});
});
// pager.js (render)
ipcRenderer.send('open-file',{
title: 'Title',
defaultPath: localStorage.getItem('defaultPath')
});
ipcRenderer.on('open-file-paths',(event,data)=>{
console.log(`Canceled? ${data.canceled}`);
console.log(`File Paths: ${data.filePaths.join(';')`);
});

How to get text from alertbox in spectron-mocha-webdiverio

I am testing my electron app using spectron.
I am trying to get text from alertbox but getting an error.
I want to get the text from alert and if that is expected then accept it.
Test :
it('Check alert message', function() {
return this.app.client
.waitForExist('#associates', 20000).should.eventually.equal(true)
.click('#pq-associates')
.click('#submit-selection')
.alertText().should.eventually.equal("You have to select any one")
.should.eventually.exist
});
I am getting the following error
no alert open
Error: An attempt was made to operate on a modal dialog when one was not open.
But I can see the alert.
As discussed with the WebDriverio community it is difficult to handle the default alert() function. They suggested to implement my own alert() like function. So using https://github.com/limonte/sweetalert2 I implemented my own alert and the test is working fine.
Community discussion : https://gitter.im/webdriverio/webdriverio/archives/2017/10/04
Spectron issue : https://github.com/electron/spectron/issues/23

Updating panel contents in a Firefox extension

I'm trying to go by the suggestion in https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/panel of using a contentScript to update the contents in my panel. Currently, I have a server that returns the html that I want to display in my panel. I do panel.postMessage("update_panel", contents); when I have the response ready, and have a contentScriptFile associated with the panel that contains
self.port.on("update_panel", handleMessage);
function handleMessage(message) {
document.write(message);
}
However, I don't see anything being updated, and I'm also unable to debug the contentScriptFile (is there an way to do so?).
What am I doing wrong?
I ended up figuring something out.
panel.port.on("updating_done", function(response) {
console.log(response);
});
panel.port.emit("update_panel", contents);
If anyone can explain why this works and postMessage doesn't, that would be great.

Getting error on $.mobile.changePage(): Uncaught TypeError: Cannot call method 'trigger' of undefined

I have a JQM apps and I am incorporating Backbone.
Since my initial javascript code is huge, I am only extracting what I believe is problematic.
I am following the advices and calls steps cited here:
jqm-config.js from http://coenraets.org/blog/2012/03/using-backbone-js-with-jquery-mobile/
http://jquerymobile.com/test/docs/pages/backbone-require.html
I have a major problem, and this is the behaviour, the problem comes from this code:
var r = Backbone.Router.extend
router: ...
"page": "pageDisplay"
...
pageDisplay: function(){
c = new AView(); // Backbone.View ...fetch() data...
$(c.el).page(); // Call to JQM to add its extra stuff; seems done correctly
$.mobile.changePage( "#" + c.id, {changeHash: false}); // line 50
}
When following the links of <a href="#page" >, I come as expected to the
page "#page" properly processed. But once there, if I click a refresh, which is indirectly reprocessed by the same router rule, I end up with the following error:
Uncaught TypeError: Cannot call method 'trigger' of undefined
I downloaded the jquery mobile development code and observed this:
// JQM1.1.2 - Line #3772 Show a specific page in the page container.
$.mobile.changePage = function( toPage, options ) {
if ( isPageTransitioning ) {
pageTransitionQueue.unshift(arguments );
return;
}
var settings = $.extend( {}, $.mobile.changePage.defaults, options);
// Make sure we have a pageContainer to work with.
settings.pageContainer = settings.pageContainer || $.mobile.pageContainer;
// Make sure we have a fromPage.
settings.fromPage = settings.fromPage || $.mobile.activePage;
// Line #3788
var mpc = settings.pageContainer, // Line #3789
pbcEvent = new $.Event("pagebeforechange" ),
triggerData = { toPage: toPage, options: settings };
// Let listeners know we're about to change the current page.
mpc.trigger( pbcEvent, triggerData ); // Line #3794
The Uncaught TypeError is caused by Line #3794, because mpc is undefined.
So, from JQM, In the Chrome inspector, I can see also that settings.fromPage is undefined and settings.pageContainer is undefined. I kind of imagine, that JQM cannot make an assumption on the fromPage, and therefore, cannot proceed on my refresh. All the options I have tried on the $mobile.changePage() have not succeed. I am out of ideas.
UPDATE/ Online site with the minimum to reproduce the problem:
apartindex, access the website with the bug
Any help will be appreciated.
The dextoInit function that calls the router code is called in $(document).ready() which does not guarantee that the jQuery mobile page has actually been set up successfully. But the router code calls $.mobile.changePage which depends on jQuery Mobile being initialized.
Putting it into mobileinit or pageinit should work.
(Unfortunately I can't modify the code and test it easily.)
Although, this fix it for the moment, it does have drawbacks. See below.
$(document).bind("pageinit", function(){
console.log('bindtomobileinit: event pageinit received');
if ( !window.AppNode.router ){
window.AppNode.router = new AppNode.singletons.router();
console.log("mobileRouter.js: Starting b history");
console.log('mobileRouter.js: About to launch Backbone history');
Backbone.history.start();
}
});
Registering to pageinit has a weird effect of being fired twice. I see that 2 nodes have been added to the Dom: the default "loading" jquery mobile div (related to pageinit:1), and my data-role page (pageinit:2). So on a "refresh browser click", my situation leaves me waiting for a first pageinit, creating an unexpected jquery mobile dom element (a default page created to display the waiting JQM circle animation), which trigger the router creation, and allows the Backbone.history call which then deal with my "" home page. The second pageinit do not interfere with the settings since I execute it only once.
I am really disappointed by this setup. I will leave this question for now, since it does sort of work.
I've found the source of the problem to be jquery-mobile version 1.3.0. When I fall back to either JSM 1.2.0 or 1.2.1, the "Uncaught TypeError: Cannot call method 'trigger' of undefined" problem goes away.
BTW, I am not using Backbone.
I had fixed this problem by using method append(), but not html()
$('body').append(view.render().$el);
I was able to resolve this issue by changing the page data property from "data-role" to "data-mobile-page" as what is referenced in line 4042 of jqm 1.3.2
fromPage.data( "mobile-page" )._trigger( "beforehide", null, { nextPage: toPage } );
Setting
$.mobile.autoInitializePage = true;
In your jquery mobile config file, some place like:
$(document).on("mobileinit", function () {...});
May help.

FB.XFBML.parse does not reparse FB widgets

I have a function call back on auth.login and would like to reparse my fb:like elements. Other actions are performed during the auth.login callback, and they execute just fine, but the .parse does not! I try executing FB.XFBML.parse(); in the console and it shows undefined and then after three seconds a console message saying 2 XFBML tags failed to render in 30000ms.
Any ideas?
For anyone who happens to run across this question running into the same problem I did...
Basically what I wanted to do was after a user logged into my website, I wanted to refresh the 'Like' button iFrame to reflect such. FB.XFBML.parse(); was only resulting in errors...so I thought "Maybe I'll just refresh the iFrame?" and since I am using jQuery as my framework - I figured I'd stick with it.
So, here's what I ended up doing:
FB.Event.subscribe('auth.login', function(response) {
if(response.status == 'connected') {
$("#login_fb").hide(); // was already doing this part
$('#like_fb > span > iframe').attr('src', function(i,val) { return val; }); // this is what I wanted to refresh
//FB.XFBML.parse(); -- THIS WAS GENERATING AN ERROR
}
});
Hopefully this will help someone else!

Resources