I began to develop addon for firefox and I had a problem.
var tabs = require('sdk/tabs');
tabs.on('ready', function (tab) {
tab.attach({
contentScript: "alert('azaza');",
onMessage: function(message) {
console.log("message");
}
});
})
When I try to execute this code in Firefox nightly 36 it says "TypeError: window is null", but in Nightly 32 it works fine! In last fierfox (not nightly) this code not working too.
I tried to execute this code in nightly's browser debugger console, but the same result (window is null).
I can see, that in sdk/tabs/utils.js browser.contentWindow is null. I think this is my window object, but why it is null?
I was able to reproduce this issue with the following code:
var { ActionButton } = require("sdk/ui/button/action");
var self = require("sdk/self");
var tabs = require('sdk/tabs');
var button = ActionButton({
icon: self.data.url("icon-16.png"),
id: "my-button",
label: "my button",
onClick: function() {
tabs.open({
url: self.data.url("text-entry.html")
});
tabs.activeTab.attach({
contentScript: "alert('azaza');"
});
}
});
To fix this issue I had to use onOpen instead of using activeTab:
var button = ActionButton({
icon: self.data.url("icon-16.png"),
id: "my-button",
label: "my button",
onClick: function() {
tabs.open({
url: self.data.url("text-entry.html"),
onOpen: function() {
tabs.activeTab.attach({
contentScript: "alert('azaza');"
});
}
});
}
});
Perhaps are you using the attach method when you cannot use it?
Related
I'm trying to do something like this in my webextension inside background script for firefox 52 browser :
Components.utils.import("resource:///modules/NewTabURL.jsm");
NewTabURL.override(value);
But firefox says that Components.utils - undefined .
My extension based on chrome , so i need to use web extension addon type (.
Is it possible to override newtab page in other way before Firefox 54 realize ?
Update : here is my little code which helps me did newtab replace , but it buggy (
var newTabUrl = browser.extension.getURL("../index.html");
function handleActivated(activeInfo) {
console.log("Tab ", activeInfo);
if (activeInfo.url === "about:newtab") {
browser.tabs.update(activeInfo.id, {
url: newTabUrl
});
}
}
var querying = browser.tabs.query({
currentWindow: true,
active: true
});
const newtabdemo = {
getActiveTab: function() {
return browser.tabs.query({
active: true,
currentWindow: true
});
},
openNewTabPage: function() {
newtabdemo.getActiveTab().then((tab) => {
var gettingInfo = browser.tabs.get(tab[0].id);
gettingInfo.then(handleActivated);
});
}
};
browser.tabs.onCreated.addListener(newtabdemo.openNewTabPage);
You can create chrome override page for the newtab page.
https://developer.chrome.com/extensions/override
https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/chrome_url_overrides
Example:
"chrome_url_overrides" : {
"newtab": "my-new-tab.html"
}
I am trying to implement cordova-plugin-email-composer.I installed the plugin using cli
cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git
I got an error Uncaught ReferenceError: require is not defined at email_composer.js:22.
In the link u can find the plugin. I added the code attached below in my index.js file. Can anyone help to solve this? Thankyou.
index.js:
bindEvents: function() {
document.addEventListener('deviceready', this.onDeviceReady, function () {
cordova.plugins.email.isAvailable(
function (isAvailable) {
alert("is email mobile available? " + (isAvailable ? "Yes" : "No"));
if(isAvailable){
window.plugin.email.open({
to: 'anu.barbie143#gmail.com',
subject: 'Greetings',
body: 'How are you? Nice greetings from Leipzig'
}, callback, scope);
}
}
);
}, false);
function callback(){
console.log("callback function");
}
function scope(){
console.log("scope function");
}
},
email_composer.js:
var exec = require('cordova/exec'),
isAndroid = navigator.userAgent.toLowerCase().indexOf('android') > -1,
mailto = 'mailto:';
In the above code i got an error require is not defined.Can anyone help me to solve this?
Thankyou.
I made it work by doing following
cordova plugin rm cordova-plugin-email-composer
then add the plugin with version 0.8.2,by following command since there is an open error in plugin version 0.8.3 for loolipop
cordova plugin add https://github.com/katzer/cordova-plugin-email-composer.git#0.8.2
index.js
var app = {
// Application Constructor
initialize: function() {
document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
},
// deviceready Event Handler
//
// Bind any cordova events here. Common events are:
// 'pause', 'resume', etc.
onDeviceReady: function() {
this.receivedEvent('deviceready');
cordova.plugins.email.open({
to: 'test#gmail.com',
cc: 'test#gmail.com',
bcc: [],
subject: 'Greetings',
body: 'How are you? Nice greetings from Naresh'
});
},
// Update DOM on a Received Event
receivedEvent: function(id) {
var parentElement = document.getElementById(id);
var listeningElement = parentElement.querySelector('.listening');
var receivedElement = parentElement.querySelector('.received');
listeningElement.setAttribute('style', 'display:none;');
receivedElement.setAttribute('style', 'display:block;');
console.log('Received Event: ' + id);
}
};
app.initialize();
Hope it helps you..
I modified your code and its working now.
So please check it once.
cordova.plugins.email.isAvailable(function (isAvailable) {
// alert('Service is not available') unless isAvailable;
alert("is email mobile available? " + (isAvailable ? "Yes" : "No"));
if(isAvailable){
window.plugin.email.open({
to: 'test#test.com',
subject: 'Greetings',
body: 'How are you? Nice greetings from Leipzig',
}, function(){
console.log('email view dismissed');
},
this);
}
});
if alert is "no" it means you don't have any email application or configuration.
I am developing a project, in which i need to call a native page in wlCommonInit()
function wlCommonInit(){
WL.NativePage.show(nativePageClassName, backFromNativePage, params);
}
I want my project to receive the direct update with persession mode. So to connect with the Mobile First Server, I have called WL.Client.connect()
function wlCommonInit(){
busyind = new WL.BusyIndicator;
busyind.show();
WL.Client.connect({onSuccess: connectSuccess, onFailure: connectFail});
WL.NativePage.show(nativePageClassName, backFromNativePage, params);
}
More over I want to handle the direct update so I have added the required code.
wl_directUpdateChallengeHandler.handleDirectUpdate = function(directUpdateData,
directUpdateContext) {
// custom WL.SimpleDialog for Direct Update
var customDialogTitle = 'Custom Title Text';
var customDialogMessage = 'Custom Message Text';
var customButtonText1 = 'Update Application';
var customButtonText2 = 'Not Now';
WL.SimpleDialog.show(customDialogTitle, customDialogMessage, [{
text: customButtonText1,
handler: function() {
directUpdateContext.start(directUpdateCustomListener);
}
}, {
text: customButtonText2,
handler: function() {
wl_directUpdateChallengeHandler.submitFailure();
}
}]);
};
var directUpdateCustomListener = {
onStart: function(totalSize) {},
onProgress: function(status, totalSize, completeSize) {},
onFinish: function(status) {
WL.SimpleDialog.show('New Update Available', 'Press reload button to update to new version', [{
text: WL.ClientMessages.reload,
handler: WL.Client.reloadApp
}]);
}
};
Here the problem is, the application is navigating to the native page
before it can go to the direct update handler function when the direct
update is available.
Is there any way to resolve it?
I think what you should do instead if use the API [WL.Client.checkForDirectUpdate.
This way you will have the ability to first check for direct update - handle it if there is an update and then execute the function for opening the native page.
The code that is running is async, so you can't control it if you're not following the above suggestion.
I am porting a Chrome Extension for FireFox using the Add-On SDK. I am using require("sdk/page-mod") to run a content script at the start of the document.
In the code, I need to close the current tab if some condition is met. In Chrome, I can send a message to the background.js file to have it close the current tab, but I am not able to figure this out for Firefox.
window.close() is very unreliable and I need to figure out a way to call a function in the main.js file from my content script.
Appreciate your help.
EDIT:
Below is my Chrome code, I need to port the same to FF AddOn SDK (FF Extension).
//in the content.js file
function closeCurrTab() {
chrome.runtime.sendMessage({action: "closeTab"}, function() {});
}
//below in the background.js file
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.action) {
case 'closeTab':
try {
chrome.tabs.getSelected(function(tab) {removeTab(tab.id);});
} catch (e) {
alert(e);
}
break;
}
}
);
function removeTab(tabId) {
try {
chrome.tabs.remove(tabId, function() {});
} catch (e) {
alert(e);
}
}
in content script:
self.port.emit("close-tab");
in main.js
PageMod({
include: "*",
contentScriptFile: "./content-script.js",
onAttach: function(worker) {
worker.port.on("close-tab", function() {
tabs.activeTab.close();
});
}
});
The following might help if you are developing an extension of firefox:
function onError(error) {
console.log(`Error: ${error}`);
}
function onRemoved() {
console.log(`Removed`);
}
function closeTabs(tabIds) {
removing = browser.tabs.remove(tabIds);
removing.then(onRemoved, onError);
}
var querying = browser.tabs.query({currentWindow: true});
querying.than(closeTabs, onError);
This will close the current tab:
require("sdk/tabs").activeTab.close();
Here's an expanded example that implements a toolbar button that closes the current tab ( silly example, I know ):
var ActionButton = require("sdk/ui/button/action").ActionButton;
var button = ActionButton({
id: "my-button-id",
label: "Close this tab",
icon: {
"16": "chrome://mozapps/skin/extensions/extensionGeneric.png"
},
onClick: function(state) {
require('sdk/tabs').activeTab.close();
}
});
For more info, please see the documentation for the tabs module.
This one is wired.
This fires from a grid toolbar button click:
// fires when the client hits the add attachment button.
onAddAttachmentClick: function () {
var uploadAttachmentsWindow = new Nipendo.ProformaInvoice.Attachment.UploadWindow({
invoice: this.invoice,
maxFileSizeInMB: this.maxFileSizeInMB
});
uploadAttachmentsWindow.on('uploadcomplete', function (win, message) {
if (message.msg !== 'success') {
return;
}
win.close();
var store = this.getStore();
store.setBaseParam('useCache', false);
store.load();
this.fireEvent(
'attachmentuploaded',
this.invoice.ProformaInvoiceNumber,
this.invoice.VendorSiteID,
this.invoice.CustomerSiteID);
}, this);
uploadAttachmentsWindow.show();
} // eo onAddAttachmentClick
This is what happens on the uploadcomplete event:
this.uploadBtn.on('click', function () {
var form = this.uploadForm.getForm();
if (!form.isValid()) {
return;
}
form.submit({
url: 'XXX.ashx',
waitMsg: Nipendo.Localization.UploadingAttachment,
scope: this,
success: function (form, action) {
this.fireEvent('uploadcomplete', this, {
msg: 'success',
response: action.response
});
},
failure: function (form, action) {
switch (action.failureType) {
case Ext.form.Action.CLIENT_INVALID:
this.fireEvent('uploadcomplete', this, {
msg: 'Form fields may not be submitted with invalid values'
});
break;
case Ext.form.Action.CONNECT_FAILURE:
this.fireEvent('uploadcomplete', this, {
msg: 'Ajax communication failed'
});
break;
case Ext.form.Action.SERVER_INVALID:
Ext.Msg.alert(action.result.title, action.result.message);
this.fireEvent('uploadcomplete', this, {
msg: action.result.message
});
break;
}
}
});
}, this);
On IE 8 I am getting this error in the debugger:
I have no idea what object is missing... from my check they are all defined.
Any idea anyone?
Notice that I have an event firing from a listener (I am suspecting it to be the root of the problem).
It is hard to see but the error occuers in ext-all.js in the fire method.
I have found the answer in : https://stackoverflow.com/a/3584887/395890
Problem was I was listing to events is 2 different windows, that is not possible in Ext.
What I have done to solv it was to call the opner window from the pop up window to notify about changes.