Set preferences in the user branch and unset them on uninstall - firefox-addon-sdk

I created a firefox add-on with the following lib/main.js:
const {Cc,Ci} = require("chrome");
var pref = Cc["#mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
pref.setIntPref("network.http.response.timeout", 3600*24);
It wasn't accepted with the following reason:
Add-ons which change critical settings must revert the changes when disabled or uninstalled. You should also make the changes in the default, rather than the user, branch.
You need to call getDefaultBranch("") on the preferences service, and call the preference methods on the returned object rather than on the preference service directly.
To revert a preference back to the default, set by setIntPref(), I found out that I have to do this on uninstall:
pref.clearUserPref("network.http.response.timeout")
This command works fine If I call it in another test-addon. I only have to find out How to implement a command, so it is executed when the firefox-addon is uninstalled?
So how do I have to understand these comments? How do I set the preferences in a "user branch"?

Here's how I did it just now:
function clearPrefBranch(aPrefBranchName) {
var defaultBranch = Services.prefs.getDefaultBranch(null);
defaultBranch.deleteBranch(aPrefBranchName);
}
Then, just call clearPrefBranch with an argument of extensions.mypluginname (assuming you used the naming convention, and you should be able to delete all of your extension's installed preferences.
EDIT:
The code I used inside of my main.js file:
const {Cc,Ci,Cm,Cr,Cu} = require("chrome");
Cu.import("resource://gre/modules/Services.jsm");
exports.onUnload = function(aOptions, aCallbacks) {
MyPlugin.shutdown();
};
function clearPrefBranch(aPrefBranchName) {
var defaultBranch = Services.prefs.getDefaultBranch(null);
defaultBranch.deleteBranch(aPrefBranchName);
}
var MyPlugin = {
shutdown: function() {
prefLoader.clearPrefBranch('extensions.oopstab');
}
};

I solved the second part (uninstall) like this, that in my main.js I added this code at the end:
exports.onUnload = function(reason) {
//called when add-on is
// uninstalled
// disabled
// shutdown
// upgraded
// downgraded
pref.clearUserPref("network.http.response.timeout");
};
That worked on disabling and uninstalling the add-on.

Related

How to run vscode extension command just right after installation?

I need to run the extension command right after the installation. There is an event like onStartupFinished, but the problem is, it will run every time the vscode starts.
How to do it? does vsocde have any way to do it?
You can use a version field in the global context state. You get the context in your activation method.
export const activate = (context: ExtensionContext): void => {
const myExtension = extensions.getExtension("<my-extension-id>");
const currentVersion = myExtension!.packageJSON.version ?? "1.0.0";
const lastVersion = context.globalState.get("MyExtensionVersion");
if (currentVersion !== lastVersion) {
void context.globalState.update("MyExtensionVersion", currentVersion);
// Do one time setup here.
}
}
Don't forget to set onStartupFinished in activationEvents.

How to determine programmatically whether an extension is disabled?

Can an extension's enabled/disabled status be determined using the Windows command line or some other method?
If an extension's settings are changed and those changes are recorded in the settings.json file and that extension is then disabled or uninstalled, those changes become 'dimmed out'. Perhaps there is a database entry somewhere that stores the dimmed status of an entry in the settings.json?
From within your own TypeScript extension you can use this:
const testedExtension = vscode.extensions.getExtension("<author of the extension>.<name of the extension>");
if (typeof testedExtension !== "undefined") {
if (testedExtension.isActive) {
vscode.window.showInformationMessage("The tested extension is active (ie enabled). :-)", {modal: true});
}
else {
vscode.window.showInformationMessage("The tested extension is NOT active (ie disabled). :-(", {modal: true});
}
}
else {
vscode.window.showInformationMessage("The tested extension was not found (ie not installed). :-( :-(", {modal: true});
}

WebIDE: How to Create Shell Plugin for Fiori Launchpad?

This is to:
customize the Fiori Launchpad (e.g. modifying shell bar or adding footer)
work in NEO environment (not Cloud Foundry)
setup in WebIDE, not Business Application Studio
Other guides have pockets of outdated information such as the no-longer-existing option to create a 'Fiori Launchpad Plugin' from 'New Project from Template'
2020 Updated
Steps:
Create SAPUI5 template app
Add following code to Component.js (& modify to your liking). Everything will be stored in Component.js, MVC will be left untouched. Everything in .then() add custom code for modifications to FLP.
init: function () {
// call the base component's init function
UIComponent.prototype.init.apply(this, arguments);
var rendererPromise= this._getRenderer();
rendererPromise.then(function(oRenderer){
// var bFull= jQuery.sap.getUriParameters().get("plugin-full");
oRenderer.addHeaderItem("sap.ushell.ui.shell.ShellHeadItem", {
icon:"sap-icon://add"
,tooltip:"Current Stage"
},true,true);
});
// var oRenderer= sap.ushell.Container.getRenderer('fiori2');
// oRenderer.addHeaderItem({icon:'sap-icon://add'},true,true);
// enable routing
// this.getRouter().initialize();
// set the device model
// this.setModel(models.createDeviceModel(), "device");
},
_getRenderer:function(){
var that=this,
oDeferred= new jQuery.Deferred(),
oRenderer;
that._oShellContainer= jQuery.sap.getObject("sap.ushell.Container");
if(!that._oShellContainer){
oDeferred.reject("Illegal state: shell container not available. This component must be executed in a unified shell runtime context.");
} else{
oRenderer= that._oShellContainer.getRenderer();
if(oRenderer){oDeferred.resolve(oRenderer);}
else{ //renderer not initialized yet, listen to rendererCreated event
that._onRendererCreated= function(oEvent){
oRenderer= oEvent.getParameter('renderer');
if(oRenderer){oDeferred.resolve(oRenderer);}
else{oDeferred.reject('Illegal state: shell renderer not available after receiving rendererLoaded event');}
};
that._oShellContainer.attachRendererCreatedEvent(that._onRendererCreated);
}
}
return oDeferred.promise();
}
Deploy your app
Activate your plugin: Go to SCP's Portal service > Go to Service > Site Directory > *Choose a launchpad you want your plugin activated in > Content Management > Apps > *Add app ('+' icon)
Properties: *App Resource: select your deployed app > *App Type: select Shell Plugin
Catalogs: *select a Catalog to place your plugin in
Save
Publish site using top ribbon's right side Globe icon
How it Looks: *Note the additional '+' button on top ribbon. Success!
References:
Sample Plugin: this sample shows how to add header action items, footer, parameters, etc.
Activating plugin: see ONLY the "Configuring a Shell Plugin App" section
API Reference on what can be modified in FLP
Instead of using jQuery in the _getRenderer function, this at least works for me in a quick test.
Uses Promises instead of Deferred and ObjectPath (requires "sap/base/util/ObjectPath") which is the recommended replacement for "$.sap.getObject" as this is deprecated.
_getRenderer: function() {
return new Promise(function(fnResolve, fnReject) {
this._oShellContainer = ObjectPath.get("sap.ushell.Container");
if (!this._oShellContainer) {
fnReject(
"Illegal state: shell container not available; this component must be executed in a unified shell runtime context."
);
} else {
var oRenderer = this._oShellContainer.getRenderer();
if (oRenderer) {
fnResolve(oRenderer);
} else {
// renderer not initialized yet, listen to rendererCreated event
this._onRendererCreated = function(oEvent) {
oRenderer = oEvent.getParameter("renderer");
if (oRenderer) {
fnResolve(oRenderer);
} else {
fnReject(
"Illegal state: shell renderer not available after receiving 'rendererLoaded' event."
);
}
};
this._oShellContainer.attachRendererCreatedEvent(
this._onRendererCreated
);
}
}
}.bind(this));
}

Possible to show users of your VSCode extension / color theme notifications on update?

Is it possible to show users of your extension or color theme notifications in Visual Studio Code? For someone who has my color theme or extension installed and is getting updates, I would like to possibly show this person a notification after they update the extension (That could be on launch of VSCode, or right after they go into the market to update & reload the extension and client themselves.)
For example: I think it would be beneficial to me and not invasive if they saw a notification after updating the extension saying "Feedback? Suggestions? Fixes?..on the theme?" OR notifying them of something changed in the theme that may not be favorable. So they can "opt out" of that change if they want (Like an extra set of borders around something or the color change of something.)
Obviously people with all notifications off would not be affected, but I thought an occasional notification after a rare update wouldn't be too bad. I have not been able to find info on if this is possible, and if it was, how to do it. Any info on this is appreciated. And if it is possible, those reading this, whether you've done it or not, would you recommend showing a notification to your theme users in that way?
Thanks :)
Show a notification on bottom-right corner, whenever your extension is updated. You can also control to show it only for major/minor releases.
That's how it looks:
Add below code to extension.ts:
import { window, ExtensionContext, extensions, env, Uri } from "vscode";
const extensionId = "jerrygoyal.shortcut-menu-bar";
// this method is called when your extension is activated
export function activate(context: ExtensionContext) {
showWhatsNew(context); // show notification in case of a major release i.e. 1.0.0 -> 2.0.0
}
// https://stackoverflow.com/a/66303259/3073272
function isMajorUpdate(previousVersion: string, currentVersion: string) {
// rain-check for malformed string
if (previousVersion.indexOf(".") === -1) {
return true;
}
//returns int array [1,1,1] i.e. [major,minor,patch]
var previousVerArr = previousVersion.split(".").map(Number);
var currentVerArr = currentVersion.split(".").map(Number);
if (currentVerArr[0] > previousVerArr[0]) {
return true;
} else {
return false;
}
}
async function showWhatsNew(context: ExtensionContext) {
const previousVersion = context.globalState.get<string>(extensionId);
const currentVersion = extensions.getExtension(extensionId)!.packageJSON
.version;
// store latest version
context.globalState.update(extensionId, currentVersion);
if (
previousVersion === undefined ||
isMajorUpdate(previousVersion, currentVersion)
) {
// show whats new notificatin:
const actions = [{ title: "See how" }];
const result = await window.showInformationMessage(
`Shortcut Menubar v${currentVersion} — Add your own buttons!`,
...actions
);
if (result !== null) {
if (result === actions[0]) {
await env.openExternal(
Uri.parse(
"https://github.com/GorvGoyl/Shortcut-Menu-Bar-VSCode-Extension#create-buttons-with-custom-commands"
)
);
}
}
}
}
You can see this implementation in my VSCode extension repo Shortcut Menu Bar
I think you can register the version during activation event and check for it on each activation. Then you can do whatever you want. For instance GitLens is migrating settings https://github.com/eamodio/vscode-gitlens/blob/master/src/extension.ts#L52 and i'm pretty sure I remember that they were opening a notification (but i have not found immediately in the code)
regards,

CKEditor, Plugin Conflict

I recently started to use CKEditor, i have come across to somewhat a weird problem, i have downloaded two plugins ,the "texttransform" and "autogrow",my config file looks like this ,,,
****CKEDITOR.editorConfig = function( config ) {
// Define changes to default configuration here. For example:
// config.language = 'fr';
// config.uiColor = '#AADC6E';
CKEDITOR.config.extraPlugins = 'texttransform'
config.extraPlugins = 'autogrow';
};****
The problem is, at one time only one plugin is active and functionality of other plugin disappears, for example,when i added autogrow, the control buttons of texttransform disappears,and they only work when i remove the line "config.extraPlugins = 'autogrow';" from my config file, any thoughts?
You are setting the configuration incorrectly. You must set config.extraPlugins only once, with two plugin names:
CKEDITOR.editorConfig = function( config ) {
config.extraPlugins = 'autogrow,texttransform';
};
See also the documentation.