Skip to content

Instantly share code, notes, and snippets.

@Noitidart
Last active August 29, 2015 13:56
Show Gist options
  • Save Noitidart/8906220 to your computer and use it in GitHub Desktop.
Save Noitidart/8906220 to your computer and use it in GitHub Desktop.
manny42_firefoxExtensionTest_lib_main.js
const {Cc, Ci, Cu, components} = require('chrome');
Cu.import('resource://gre/modules/Services.jsm');
var utils = require("sdk/window/utils");
var tabs = require("sdk/tabs");
var self = require("sdk/self");
var data = self.data;
function createPanel() {
var winsize = tabs.activeTab.attach({
contentScriptFile: [data.url('windowsize.js')]
});
winsize.port.on('winsize', function (param) {
var popover = require("sdk/panel").Panel({
width: param.width - 100,
height: param.height - 100,
top: 100,
left: 100,
contentURL: data.url("text.html"),
contentScriptFile: [data.url("jquery-1.10.2.min.js"),
data.url("test.js")
],
});
popover.show();
Cu.reportError('NOIT - panel should be showing')
Cu.reportError('NOIT - data url of text.html = "' + data.url("text.html") + '"');
/* cDump({
obj:popover.constructor.attributes,
deep:1,
title:'popover.constructor.attributes',
inBackground:true
}); */
var aDOMWindow = Services.wm.getMostRecentWindow('navigator:browser');
var mainPopupSet = aDOMWindow.document.querySelector('#mainPopupSet');
var mainPopupSetArr = [];
[].forEach.call(mainPopupSet.childNodes, function(node) {
Cu.reportError('pushing');
mainPopupSetArr.push(node);
});
mainPopupSetArr.reverse();
//our panel should be the last one in mainPopupSet but we'll double check it
//it should be mainPopupSet.childNodes[mainPopupSet.childNodes.length-1] but we'll go through the loop do dbl check
//i verify it by checking if the iframe content matches what we have above
var BreakException= {};
try {
[].forEach.call(mainPopupSetArr, function(cNode) {
//Cu.reportError('NOIT - the node name of cNode is "' + cNode.nodeName + '"');
if (cNode.nodeName != 'panel') {
//this cannot be our node because its not a panel
return; //eqivalent of continue in regular for loop
}
var iframe = cNode.querySelector('iframe');
if (iframe) {
//ok it has iframe now lets check src
//lets check if this iframe is currently loading, if its already loaded, then attaching a DOMContentLoaded listener to it will never fire
var readyState = iframe.contentDocument.readyState;
//Cu.reportError('NOIT - iframe readysate = ' + readyState);
if (readyState == 'complete') {
//this is probably not ours becauuse we just added iframe so it should be at uninitialized
//but lets check anyways
//return; //debug: for now not checking
Cu.reportError('NOIT - in complete readystate if block');
var loc = iframe.contentWindow.document.location + ''; //add the + '' to end to make the location a string, so can now use string functions like indexOf
Cu.reportError('NOIT - location of this completed loading iframe is = "' + loc + '"');
if (loc == data.url("text.html")) {
Cu.reportError('NOIT - this is our iframe so this is our panel!');
doThisToOurPanel(cNode);
}
} else if (readyState == 'uninitialized') {
//this is likely our frame lets add event listener to listen to this sucker
Cu.reportError('NOIT - in complete uninit if block');
iframe.contentDocument.onreadystatechange = function() {
Cu.reportError('NOIT - READY STATE CHANGINGGGGG');
Cu.reportError('onreadystatechange in iframe the readystate is now ' + iframe.contentDocument.readyState)
}
Cu.reportError('NOIT - added the onreadystatechange func but lets see readyState now it is = ' + iframe.contentDocument.readyState);
//todo: FOR SOME WEIRD REASON I CANT FIGURE OUT the onreadystatechange function never fires!!!!
iframe.addEventListener('DOMContentLoaded', function(){
//this func checks the iframe location to see if it is ours
Cu.reportError('NOIT - DOMContentLoaded fired!');
iframe.removeEventListener('DOMContentLoaded', arguments.callee, false); //this removes the DOMContentLoaded listener i added on line 49
Cu.reportError('readystae of this iframe is ' + iframe.contentDocument.readyState);
var loc = iframe.contentWindow.document.location + ''; //add the + '' to end to make the location a string, so can now use string functions like indexOf
//iframe.contentWindow.alert(iframe.getAttribute('src'));
Cu.reportError('location of this iframe is == ' + loc);
if (loc == data.url("text.html")) {
Cu.reportError('NOIT - this is our iframe so this is our panel!');
doThisToOurPanel(cNode);
}
Cu.reportError('NOIT - also want to see src which is = ' + iframe.getAttribute('src'));
}, false);
Cu.reportError('NOIT - finished uninit if block');
}
} else {
//Cu.reportError('NOIT - no iframe in this')
}
});
} catch (ex) {
//if (ex!==BreakException) throw ex;
throw ex;
}
Cu.reportError('NOIT - finished')
popover.port.emit("ready", "toto");
});
winsize.port.emit('fetchwinsize');
}
require("sdk/widget").Widget({
label: "Test Panel",
id: "panel",
contentURL: "http://www.mozilla.org/favicon.ico",
onClick: createPanel
});
function mypopupIsHiding(e) {
Cu.reportError('my popup is hiding will try to prevent');
e.stopPropagation();
e.preventDefault();
}
function doThisToOurPanel(panel) {
panel.setAttribute('noautohide','true');
Cu.reportError('noautohide set to true!');
panel.addEventListener('popuphiding', mypopupIsHiding, false);
var btn = panel.ownerDocument.createElement('button');
btn.setAttribute('label','close this panel');
btn.addEventListener('click',function(){
panel.removeEventListener('popuphiding', mypopupIsHiding, false);
panel.hidePopup();
})
panel.appendChild(btn);
}
function cDump(args) {
//oldway: obj, title, deep, outputTarget
//args is object with keys:
//obj - required, the obj to dump
//title - optional, if given, the cdump will be titled
//deep - optional, if given it will go deeper in //not implemented yet: integer of how many levels deep to go
//target - where to dump
//if target == 0, can define inBackground: true
//if target not in targetsDisableDeep then can specify initDisplayed to true which will make all sub divs be block
//inBackground set to tru to load tab in bg
//Services jsm must be imported
//set args.deep to 1 to make it args.deep but initialize args.deeps div at none.
//se args.deep to 2 to initialize at block
//args.target == 0 then new tab, if set args.target to false then will do 0 but will load tab in background, if set to 0 or leave undefined it will load tab in foreground
//args.target == 1 then reportError (cannot do args.deep in this args.target)
//args.target == 2 then new window (not yet setup)
//args.target == 3 then Services.console.logStringMessage
//args.target == nsIFile, file at that path
var targetsDisableDeep = [1,3];
var tstr = '';
var bstr = '';
if (args.deep && targetsDisableDeep.indexOf(args.target) == -1) {
bstr = '<a href="javascript:void(0)" onclick="var subdivs = document.querySelectorAll(\'div > div\'); for(var i=0;i<subdivs.length;i++) { subdivs[i].style.display = subdivs[i].style.display==\'block\'?\'none\':\'block\'; }">Toggle All</a>\n\n';
}
if (!args.deep) {
args.deep = 0;
}
var fstr = '';
for (var b in args.obj) {
try{
bstr += b+'='+args.obj[b]+'\n';
if (args.deep && targetsDisableDeep.indexOf(args.target) == -1) {
var sstr = '';
for (var c in args.obj[b]) {
try {
sstr += '\t\t\t' + c+'='+args.obj[b][c]+'\n';
} catch(e0) {
sstr += '\t\t\t' + c+'=exception_occured_on_read='+e0+'\n';
}
}
if (sstr != '') {
bstr += '<div style="margin-left:35px;color:gray;cursor:pointer;border:1px solid blue;" onclick="this.childNodes[1].style.display=this.childNodes[1].style.display==\'block\'?\'none\':\'block\';window.setScroll(window.scrollX,this.offsetTop-this.offsetHeight);">click to toggle<div style="display:' + (args.initDisplayed ? 'block' : 'none') + ';">' + sstr + '</div></div>';
}
}
} catch (e) {
fstr = b+'='+e+'\n';
}
}
tstr += '<b>BSTR::</b>\n' + bstr;
tstr += '\n<b>FSTR::</b>\n' + fstr;
if (!args.target) {
var cWin = Services.wm.getMostRecentWindow('navigator:browser');
var onloadFunc = function() {
//cWin.gBrowser.selectedTab = cWin.gBrowser.tabContainer.childNodes[cWin.gBrowser.tabContainer.childNodes.length-1];
newTabBrowser.removeEventListener('load', onloadFunc, true);
if (args.title) { newTabBrowser.contentDocument.title = args.title; }
newTabBrowser.contentDocument.body.innerHTML = tstr.replace(/\n/g,'<br>')
};
var newTabBrowser = cWin.gBrowser.getBrowserForTab(cWin.gBrowser.loadOneTab('about:blank',{inBackground:args.inBackground}));
newTabBrowser.addEventListener('load', onloadFunc, true);
} else if (args.target == 1) {
tstr = 'CDUMP OF "' + args.title + '">>>\n\n' + tstr + ' "\n\nEND: CDUMP OF "' + args.title + '" ^^^';
Cu.reportError(tstr);
} else if (args.target == 2) {
//to new window
} else if (args.target == 3) {
tstr = 'CDUMP OF "' + args.title + '">>>\n\n' + tstr + ' "\n\nEND: CDUMP OF "' + args.title + '" ^^^';
Services.console.logStringMessage(tstr);
} else if (args.target instanceof Ci.nsIFile) {
var html = '<div style="display:flex;align-items:flex-start;broder:1px dashed black;"><div style="width:15em;font-weight:bold;display:inline-block;">' + [new Date().toLocaleTimeString(), 'Title: ' + args.title].join('<br>') + '</div><div class="dumpCol" style="display:inline-block;>' + tstr.replace(/\n/g,'<br>') + '</div></div>';
writeFile(args.target, html, false, function(status) {
if (!Components.isSuccessCode(status)) {
Services.wm.getMostRecentWindow(null).alert('cDump to file failed');
} else {
//Services.wm.getMostRecentWindow(null).alert('writeFile SUCCESFUL');
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment