Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 #10

Merged
merged 8 commits into from
Jul 9, 2014
Merged

#6 #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
294 changes: 227 additions & 67 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function winWorker(aDOMWindow) {
this.DOMWindow = aDOMWindow;
this.DOMDocument = this.DOMWindow.document;
this.gBrowser = this.DOMWindow.gBrowser;
this.gThrobber = this.DOMDocument.getElementById('navigator-throbber');
this.gThrobber = this.DOMDocument.getElementById('throbber-restored');


this.gMutationFunc = function(ms) {
Expand Down Expand Up @@ -96,47 +96,89 @@ var observers = {
if (aData == self.aData.id) {
console.log('IS THROBBER RESTORED');
var doc = aSubject;

/*start - set up xul on custom images settings*/
var custImgIdle = doc.querySelector('setting[pref="extensions.ThrobberRestored.customImgIdle"]');
var custImgLoading = doc.querySelector('setting[pref="extensions.ThrobberRestored.customImgLoading"]');
var custImgSettings = {customImgIdle: custImgIdle, customImgLoading: custImgLoading}; //key = pref name in prefs object value is the setting xul element

var props = {
id: 'btn_resetCustImgIdle',
label: 'Restore Default'
};
var preExEl = doc.querySelector('#' + props.id);
if (preExEl) { //label is already there so continue, so remove it then we'll add again
preExEl.parentNode.removeChild(preExEl);
}
var el = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'button');
el.addEventListener('command', function() {
prefs.customImgIdle.setval('');
}, false);
for (var p in props) {
el.setAttribute(p, props[p]);
for (var n in custImgSettings) {
var props = {
id: 'resetBtn_' + n,
label: 'Restore Default',
anonid: 'resetbtn'
};
if (prefs[n].value == '') {
props.style = 'display:none;'
}
var preExEl = doc.querySelector('#' + props.id);
if (preExEl) { //label is already there so continue, so remove it then we'll add again
preExEl.parentNode.removeChild(preExEl);
}
var el = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'button');

console.log('starting bind');
var bound = function(settingPrefName, settingXUL) {
console.log('calling the bound func');
prefs[settingPrefName].setval('');
settingXUL.inputChanged();
}.bind(null, n, custImgSettings[n]);
console.log('adding event listener to ' + n);
el.addEventListener('command', bound, false);
for (var p in props) {
el.setAttribute(p, props[p]);
}
var browseBtn = doc.getAnonymousElementByAttribute(custImgSettings[n], 'anonid', 'button');
browseBtn.parentNode.insertBefore(el, browseBtn);
//start the oninput changed method
var setattr = '';
//setattr += 'alert(\'starting ' + n + '\');';
setattr += 'var resetBtn = document.getAnonymousElementByAttribute(this, \'anonid\', \'resetbtn\');';
setattr += 'var img = document.getAnonymousElementByAttribute(this, \'anonid\', \'preview\');';
setattr += 'if (this.value != "") {'
setattr += 'resetBtn.style.display = \'\';';
setattr += 'resetBtn.style.display = \'\';';
setattr += 'img.src = Services.io.newFileURI(new FileUtils.File(this.value)).spec;';
setattr += 'img.setAttribute(\'value\', this.value);';
setattr += '} else {';
setattr += 'resetBtn.style.display = \'none\';';
setattr += 'img.src = \'\';';
setattr += '}';
//setattr += 'alert(\'done\');';
custImgSettings[n].setAttribute('oninputchanged', setattr);
//end the oninput changed method

//add idle img preview
var browseLbl = doc.getAnonymousElementByAttribute(custImgSettings[n], 'anonid', 'input');
browseLbl.style.display = 'none';

var el = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'image');
var props = {
id: 'imgPreview_' + n,
onclick: 'alert(\'Original Path of Image on Disk: "\' + this.getAttribute(\'value\') + \'"\')',
anonid: 'preview',
src: '',
value: ''
};
var preExEl = doc.querySelector('#' + props.id);
if (preExEl) { //so remove it then we'll add again (just in case this is an update or something and something changed)
preExEl.parentNode.removeChild(preExEl);
}
if (prefs[n].value != '') {
var normalized = OS.Path.normalize(prefs[n].value);
//var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, OS.Path.basename(normalized));
var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, 'throbber-restored-' + n);
props.src = Services.io.newFileURI(new FileUtils.File(profRootDirLoc)).spec;
props.value = prefs[n].value;
}
for (var p in props) {
el.setAttribute(p, props[p]);
}
browseLbl.parentNode.insertBefore(el, browseLbl);
//end idle img preview
}
var browseBtn = doc.getAnonymousElementByAttribute(custImgIdle, 'anonid', 'button');
browseBtn.parentNode.insertBefore(el, browseBtn);

/*end - set up xul on custom images settings*/


//add loading reste button
var props = {
id: 'btn_resetCustImgLoading',
label: 'Restore Default'
};
var preExEl = doc.querySelector('#' + props.id);
if (preExEl) { //label is already there so continue, so remove it then we'll add again
preExEl.parentNode.removeChild(preExEl);
}
var el = doc.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'button');
el.addEventListener('command', function() {
prefs.customImgLoading.setval('');
}, false);
for (var p in props) {
el.setAttribute(p, props[p]);
}
var browseBtn = doc.getAnonymousElementByAttribute(custImgLoading, 'anonid', 'button');
browseBtn.parentNode.insertBefore(el, browseBtn);
}
},
reg: function () {
Expand Down Expand Up @@ -214,7 +256,7 @@ var windowListener = {
return;
}

/* var throbber = aDOMWindow.document.getElementById('navigator-throbber');
/* var throbber = aDOMWindow.document.getElementById('throbber-restored');
if (throbber) {

} */
Expand Down Expand Up @@ -261,15 +303,15 @@ function startup(aData, aReason) {
}

CustomizableUI.createWidget({ //must run createWidget before windowListener.register because the register function needs the button added first
id: 'navigator-throbber',
id: 'throbber-restored',
type: 'custom',
defaultArea: CustomizableUI.AREA_NAVBAR,
onBuild: function(aDocument) {
var toolbaritem = aDocument.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'toolbaritem');
var image = aDocument.createElementNS('http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul', 'image');

var props = {
id: 'navigator-throbber',
id: 'throbber-restored',
title: 'Activity Indicator',
align: 'center',
pack: 'center',
Expand Down Expand Up @@ -308,7 +350,7 @@ function shutdown(aData, aReason) {
console.log('s0');
windowListener.unregister();
console.log('s1');
CustomizableUI.destroyWidget('navigator-throbber');
CustomizableUI.destroyWidget('throbber-restored');
console.log('s2');
myServices.sss.unregisterSheet(cssUri, myServices.sss.USER_SHEET); //running htis last as i think its syncronus
console.log('s3');
Expand Down Expand Up @@ -343,6 +385,26 @@ function install(aData, aReason) {}
function uninstall(aData, aReason) {
if (aReason == ADDON_UNINSTALL) { //have to put this here because uninstall fires on upgrade/downgrade too
//this is real uninstall
//if custom images were used lets delete them now
var customImgPrefs = ['customImgIdle', 'customImgLoading'];
[].forEach.call(customImgPrefs, function(n) {
//cant check the pref i guess because it may be unintialized or deleted before i used have a `if(prefs[n].value != '') {`
//var normalized = OS.Path.normalize(prefs[n].value);
//var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, OS.Path.basename(normalized));
var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, 'throbber-restored-' + n);
var promiseDelete = OS.File.remove(profRootDirLoc);
console.log('profRootDirLoc', profRootDirLoc)
promiseDelete.then(
function() {
//Services.prompt.alert(null, 'deleted', 'success on ' + n);
},
function(aRejReas) {
console.warn('Failed to delete copy of custom throbber ' + n + ' image for reason: ', aRejReas);
//Services.prompt.alert(null, 'deleted', 'FAILED on ' + n);
}
);
});

Services.prefs.deleteBranch(prefPrefix);
}
}
Expand All @@ -356,28 +418,77 @@ var prefs = { //each key here must match the exact name the pref is saved in the
value: null,
type: 'Char',
onChange: function(oldVal, newVal, refObj) {
var msga = '';
if (oldVal === null) {
msga = 'register/init';
} else if (oldVal == newVal) {
msga = 'probably a programmatic force';
} else if (oldVal != newVal) {
msga = 'really chaning';
}
//Services.prompt.alert(null, 'prefChange - ' + refObj.name, msga);
if (oldVal && oldVal != '') {
myServices.sss.unregisterSheet(cssUri_CustomImgIdle, myServices.sss.USER_SHEET);
//Services.prompt.alert(null, 'sheet unreg', 'old sheet unrgistered');
}
newVal = newVal.trim();
if (newVal == '') {
cssUri_CustomImgIdle = '';
if (oldVal !== null && oldVal != '') {
//lets delete the old one from profile folder
var normalized = OS.Path.normalize(oldVal);
//var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, OS.Path.basename(normalized));
var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, 'throbber-restored-customImgIdle');
var promiseDelete = OS.File.remove(profRootDirLoc);
promiseDelete.then(
function() {
//Services.prompt.alert(null, 'deleted', 'success');
},
function(aRejReas) {
console.warn('Failed to delete copy of custom throbber IDLE image for reason: ', aRejReas);
//Services.prompt.alert(null, 'deleted', 'FAILED');
}
);
}
} else {
var normalized = OS.Path.normalize(newVal);
var file = new FileUtils.File(normalized);
var fileuri = Services.io.newFileURI(file).spec;
console.log('fileuri', fileuri);
//var newuri = Services.io.newURI(newVal, null, null);
//var newValRep = 'file:///' + newuri.spec.replace(/\\/g, '/');
//var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, OS.Path.basename(normalized));
var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, 'throbber-restored-customImgIdle');

var applyIt = function() {
var file = new FileUtils.File(profRootDirLoc);
var fileuri = Services.io.newFileURI(file).spec;
console.log('fileuri', fileuri);
//var newuri = Services.io.newURI(newVal, null, null);
//var newValRep = 'file:///' + newuri.spec.replace(/\\/g, '/');

var css = '#throbber-restored:not([loading]) { list-style-image: url("' + fileuri + '#' + Math.random() + '") !important; }';
var newURIParam = {
aURL: 'data:text/css,' + encodeURIComponent(css),
aOriginCharset: null,
aBaseURI: null
};
cssUri_CustomImgIdle = Services.io.newURI(newURIParam.aURL, newURIParam.aOriginCharset, newURIParam.aBaseURI);
myServices.sss.loadAndRegisterSheet(cssUri_CustomImgIdle, myServices.sss.USER_SHEET); //running this last as i think its syncronus
}

var css = '#navigator-throbber:not([loading]) { list-style-image: url("' + fileuri + '") !important; }';
var newURIParam = {
aURL: 'data:text/css,' + encodeURIComponent(css),
aOriginCharset: null,
aBaseURI: null
};
cssUri_CustomImgIdle = Services.io.newURI(newURIParam.aURL, newURIParam.aOriginCharset, newURIParam.aBaseURI);
myServices.sss.loadAndRegisterSheet(cssUri_CustomImgIdle, myServices.sss.USER_SHEET); //running this last as i think its syncronus
if (oldVal !== null) {
//lets copy it to profile folder
var promiseCopy = OS.File.copy(normalized, profRootDirLoc);
promiseCopy.then(
function() {
console.log('copy completed succesfully');
applyIt();
},
function(aRejReas) {
console.error('copy failed reason: ', aRejReas);
throw new Error('FAILED TO COPY IMAGE TO PROFILE ROOT DIRECTORY');
}
);
} else {
console.log('just going to directly apply it');
applyIt();
}
}
}
},
Expand All @@ -386,29 +497,78 @@ var prefs = { //each key here must match the exact name the pref is saved in the
value: null,
type: 'Char',
onChange: function(oldVal, newVal, refObj) {
var msga = '';
if (oldVal === null) {
msga = 'register/init';
} else if (oldVal == newVal) {
msga = 'probably a programmatic force';
} else if (oldVal != newVal) {
msga = 'really chaning';
}
//Services.prompt.alert(null, 'prefChange - ' + refObj.name, msga);
if (oldVal && oldVal != '') {
console.log('cssUri_CustomImgLoading', cssUri_CustomImgLoading);
myServices.sss.unregisterSheet(cssUri_CustomImgLoading, myServices.sss.USER_SHEET);
//Services.prompt.alert(null, 'sheet unreg', 'old sheet unrgistered');
}
newVal = newVal.trim();
if (newVal == '') {
cssUri_CustomImgLoading = '';
if (oldVal !== null && oldVal != '') {
//lets delete the old one from profile folder
var normalized = OS.Path.normalize(oldVal);
//var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, OS.Path.basename(normalized));
var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, 'throbber-restored-customImgLoading');
var promiseDelete = OS.File.remove(profRootDirLoc);
promiseDelete.then(
function() {
//Services.prompt.alert(null, 'deleted', 'success');
},
function(aRejReas) {
console.warn('Failed to delete copy of custom throbber LOADING image for reason: ', aRejReas);
//Services.prompt.alert(null, 'deleted', 'FAILED');
}
);
}
} else {
var normalized = OS.Path.normalize(newVal);
var file = new FileUtils.File(normalized);
var fileuri = Services.io.newFileURI(file).spec;
console.log('fileuri', fileuri);
//var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, OS.Path.basename(normalized));
var profRootDirLoc = OS.Path.join(OS.Constants.Path.profileDir, 'throbber-restored-customImgLoading');

//var newuri = Services.io.newURI(newVal, null, null);
//var newValRep = 'file:///' + newuri.spec.replace(/\\/g, '/');
var applyIt = function() {
var file = new FileUtils.File(profRootDirLoc);
var fileuri = Services.io.newFileURI(file).spec;
console.log('fileuri', fileuri);
//var newuri = Services.io.newURI(newVal, null, null);
//var newValRep = 'file:///' + newuri.spec.replace(/\\/g, '/');

var css = '#throbber-restored[loading] { list-style-image: url("' + fileuri + '#' + Math.random() + '") !important; }';
var newURIParam = {
aURL: 'data:text/css,' + encodeURIComponent(css),
aOriginCharset: null,
aBaseURI: null
};
cssUri_CustomImgLoading = Services.io.newURI(newURIParam.aURL, newURIParam.aOriginCharset, newURIParam.aBaseURI);
myServices.sss.loadAndRegisterSheet(cssUri_CustomImgLoading, myServices.sss.USER_SHEET); //running this last as i think its syncronus
}

var css = '#navigator-throbber[loading] { list-style-image: url("' + fileuri + '") !important; }';
var newURIParam = {
aURL: 'data:text/css,' + encodeURIComponent(css),
aOriginCharset: null,
aBaseURI: null
};
cssUri_CustomImgLoading = Services.io.newURI(newURIParam.aURL, newURIParam.aOriginCharset, newURIParam.aBaseURI);
myServices.sss.loadAndRegisterSheet(cssUri_CustomImgLoading, myServices.sss.USER_SHEET); //running this last as i think its syncronus
if (oldVal !== null) {
//lets copy it to profile folder
var promiseCopy = OS.File.copy(normalized, profRootDirLoc);
promiseCopy.then(
function() {
console.log('copy completed succesfully');
applyIt();
},
function(aRejReas) {
console.error('copy failed reason: ', aRejReas);
throw new Error('FAILED TO COPY IMAGE TO PROFILE ROOT DIRECTORY');
}
);
} else {
console.log('just going to directly apply it');
applyIt();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

<!-- Front End MetaData -->
<em:name>Throbber Restored</em:name>
<em:description>Restores the activity indicator, also known as throbber, to Australis.</em:description>
<em:description>Restores the activity indicator, also known as throbber, to Australis. Can use default images from FF28 or load your own.</em:description>
<em:creator>Noitidart</em:creator>
<em:creator>PimpUigi for Idea</em:creator>
</Description>
Expand Down
Loading