1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-10-04 16:47:15 +02:00

Firefox/Fennec code cleanup

This commit is contained in:
Deathamns 2015-02-28 20:18:58 +01:00
parent aa0e713c79
commit dc8623f1e8
5 changed files with 301 additions and 389 deletions

View File

@ -53,6 +53,7 @@ const contentObserver = {
MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT, MAIN_FRAME: Ci.nsIContentPolicy.TYPE_DOCUMENT,
contentBaseURI: 'chrome://' + hostName + '/content/js/', contentBaseURI: 'chrome://' + hostName + '/content/js/',
cpMessageName: hostName + ':shouldLoad', cpMessageName: hostName + ':shouldLoad',
ignoredPopups: new WeakMap(), ignoredPopups: new WeakMap(),
uniqueSandboxId: 1, uniqueSandboxId: 1,

View File

@ -23,12 +23,12 @@
</r:Description> </r:Description>
</targetApplication> </targetApplication>
<!-- Firefox for Android --> <!-- Fennec -->
<targetApplication> <targetApplication>
<r:Description> <r:Description>
<id>{{aa3c5121-dab2-40e2-81ca-7ea25febc110}}</id> <id>{{aa3c5121-dab2-40e2-81ca-7ea25febc110}}</id>
<minVersion>24.0</minVersion> <minVersion>24.0</minVersion>
<maxVersion>38.0</maxVersion> <maxVersion>39.0</maxVersion>
</r:Description> </r:Description>
</targetApplication> </targetApplication>
@ -40,23 +40,5 @@
<maxVersion>2.36</maxVersion> <maxVersion>2.36</maxVersion>
</r:Description> </r:Description>
</targetApplication> </targetApplication>
<!-- Firefox for Android
<targetApplication>
<r:Description>
<id>{{aa3c5121-dab2-40e2-81ca-7ea25febc110}}</id>
<minVersion>24.0</minVersion>
<maxVersion>39.0</maxVersion>
</r:Description>
</targetApplication> -->
<!-- Thunderbird
<targetApplication>
<r:Description>
<id>{{3550f703-e582-4d05-9a08-453d09bdfdc6}}</id>
<minVersion>24.0</minVersion>
<maxVersion>39.0</maxVersion>
</r:Description>
</targetApplication> -->
</r:Description> </r:Description>
</r:RDF> </r:RDF>

View File

@ -38,12 +38,8 @@ const {Services} = Cu.import('resource://gre/modules/Services.jsm', null);
/******************************************************************************/ /******************************************************************************/
var vAPI = self.vAPI = self.vAPI || {}; var vAPI = self.vAPI = self.vAPI || {};
vAPI.firefox = true; vAPI.firefox = true;
vAPI.fennec = Services.appinfo.ID === '{aa3c5121-dab2-40e2-81ca-7ea25febc110}';
vAPI.fennec = Components.classes["@mozilla.org/xre/app-info;1"]
.getService(Components.interfaces.nsIXULAppInfo)
.ID == "{aa3c5121-dab2-40e2-81ca-7ea25febc110}";
/******************************************************************************/ /******************************************************************************/
@ -266,23 +262,30 @@ var windowWatcher = {
return; return;
} }
if ( this.gBrowser && this.gBrowser.tabContainer ) { var tabContainer;
// desktop Firefox var tabBrowser = getTabBrowser(this);
var tC = this.gBrowser.tabContainer;
this.gBrowser.addTabsProgressListener(tabWatcher); if ( !tabBrowser ) {
tC.addEventListener('TabClose', tabWatcher.onTabClose); return;
tC.addEventListener('TabSelect', tabWatcher.onTabSelect);
} else if ( this.BrowserApp && this.BrowserApp.deck ) {
// Fennec
var deck = this.BrowserApp.deck;
deck.addEventListener('DOMTitleChanged', tabWatcher.onFennecLocationChange);
deck.addEventListener('TabClose', tabWatcher.onTabClose);
deck.addEventListener('TabSelect', tabWatcher.onTabSelect);
} }
if ( tabBrowser.deck ) {
// Fennec
tabContainer = tabBrowser.deck;
tabContainer.addEventListener(
'DOMTitleChanged',
tabWatcher.onFennecLocationChange
);
} else if ( tabBrowser.tabContainer ) {
// desktop Firefox
tabContainer = tabBrowser.tabContainer;
tabBrowser.addTabsProgressListener(tabWatcher);
} else {
return;
}
tabContainer.addEventListener('TabClose', tabWatcher.onTabClose);
tabContainer.addEventListener('TabSelect', tabWatcher.onTabSelect);
vAPI.contextMenu.register(this.document); vAPI.contextMenu.register(this.document);
// when new window is opened TabSelect doesn't run on the selected tab? // when new window is opened TabSelect doesn't run on the selected tab?
@ -298,26 +301,23 @@ var windowWatcher = {
/******************************************************************************/ /******************************************************************************/
var tabWatcher = { var tabWatcher = {
onTabClose: function({target: tab}) { SAME_DOCUMENT: Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT,
var tabId = vAPI.tabs.getTabId(tab);
onTabClose: function({target}) {
// target is tab in Firefox, browser in Fennec
var tabId = vAPI.tabs.getTabId(target);
vAPI.tabs.onClosed(tabId); vAPI.tabs.onClosed(tabId);
delete vAPI.toolbarButton.tabs[tabId]; delete vAPI.toolbarButton.tabs[tabId];
}, },
onTabSelect: function({target: tab}) { onTabSelect: function({target}) {
var URI = null; // target is tab in Firefox, browser in Fennec
if ( tab.currentURI ) { var URI = (target.linkedBrowser || target).currentURI;
// on Fennec the target is actually the linked browser
URI = tab.currentURI;
} else {
// desktop Firefox
URI = tab.linkedBrowser.currentURI;
}
var aboutPath = URI.schemeIs('about') && URI.path; var aboutPath = URI.schemeIs('about') && URI.path;
var tabId = vAPI.tabs.getTabId(tab); var tabId = vAPI.tabs.getTabId(target);
if ( !aboutPath || (aboutPath !== 'blank' && aboutPath !== 'newtab') ) { if ( !aboutPath || (aboutPath !== 'blank' && aboutPath !== 'newtab') ) {
vAPI.setIcon(tabId, tab.ownerDocument.defaultView); vAPI.setIcon(tabId, getOwnerWindow(target));
return; return;
} }
@ -336,7 +336,7 @@ var tabWatcher = {
var tabId = vAPI.tabs.getTabId(browser); var tabId = vAPI.tabs.getTabId(browser);
// LOCATION_CHANGE_SAME_DOCUMENT = "did not load a new document" // LOCATION_CHANGE_SAME_DOCUMENT = "did not load a new document"
if ( flags & Ci.nsIWebProgressListener.LOCATION_CHANGE_SAME_DOCUMENT ) { if ( flags & this.SAME_DOCUMENT ) {
vAPI.tabs.onUpdated(tabId, {url: location.asciiSpec}, { vAPI.tabs.onUpdated(tabId, {url: location.asciiSpec}, {
frameId: 0, frameId: 0,
tabId: tabId, tabId: tabId,
@ -354,23 +354,22 @@ var tabWatcher = {
}); });
}, },
onFennecLocationChange: function(e) { onFennecLocationChange: function({target: doc}) {
// Fennec "equivalent" to onLocationChange // Fennec "equivalent" to onLocationChange
// note that DOMTitleChanged is selected as it fires very early // note that DOMTitleChanged is selected as it fires very early
// (before DOMContentLoaded), and it does fire even if there is no title // (before DOMContentLoaded), and it does fire even if there is no title
var tabId = vAPI.tabs.getTabId(e.target); var win = doc.defaultView;
if ( tabId === -1 ) { if ( win !== win.top ) {
// probably not top level
return; return;
} }
vAPI.tabs.onNavigation({ vAPI.tabs.onNavigation({
frameId: 0, frameId: 0,
tabId: tabId, tabId: getOwnerWindow(win).getTabForWindow(win).id,
url: e.target.location.href url: Services.io.newURI(win.location.href, null, null).asciiSpec
}); });
}, }
}; };
/******************************************************************************/ /******************************************************************************/
@ -383,6 +382,37 @@ vAPI.noTabId = '-1';
/******************************************************************************/ /******************************************************************************/
var getTabBrowser = function(win) {
return vAPI.fennec && win.BrowserApp || win.gBrowser || null;
};
/******************************************************************************/
var getBrowserForTab = function(tab) {
return vAPI.fennec && tab.browser || tab.linkedBrowser || null;
};
/******************************************************************************/
var getOwnerWindow = function(target) {
if ( target.ownerDocument ) {
return target.ownerDocument.defaultView;
}
// Fennec
for ( var win of vAPI.tabs.getWindows() ) {
for ( var tab of win.BrowserApp.tabs) {
if ( tab === target || tab.window === target ) {
return win;
}
}
}
return null;
};
/******************************************************************************/
vAPI.tabs = {}; vAPI.tabs = {};
/******************************************************************************/ /******************************************************************************/
@ -405,37 +435,36 @@ vAPI.tabs.registerListeners = function() {
vAPI.contextMenu.unregister(win.document); vAPI.contextMenu.unregister(win.document);
win.removeEventListener('DOMContentLoaded', windowWatcher.onReady); win.removeEventListener('DOMContentLoaded', windowWatcher.onReady);
if ( win.gBrowser && win.gBrowser.tabContainer ) { var tabContainer;
// desktop Firefox var tabBrowser = getTabBrowser(win);
var tC = win.gBrowser.tabContainer; if ( !tabBrowser ) {
continue;
}
win.gBrowser.removeTabsProgressListener(tabWatcher); if ( tabBrowser.deck ) {
tC.removeEventListener('TabClose', tabWatcher.onTabClose);
tC.removeEventListener('TabSelect', tabWatcher.onTabSelect);
// close extension tabs
for ( var tab of win.gBrowser.tabs ) {
var URI = tab.linkedBrowser.currentURI;
if ( URI.schemeIs('chrome') && URI.host === location.host ) {
win.gBrowser.removeTab(tab);
}
}
} else if ( win.BrowserApp && win.BrowserApp.deck ) {
// Fennec // Fennec
var deck = win.BrowserApp.deck; tabContainer = tabBrowser.deck;
tabContainer.removeEventListener(
'DOMTitleChanged',
tabWatcher.onFennecLocationChange
);
} else if ( tabBrowser.tabContainer ) {
tabContainer = tabBrowser.tabContainer;
tabBrowser.removeTabsProgressListener(tabWatcher);
}
deck.removeEventListener('DOMTitleChanged', tabWatcher.onFennecLocationChange); tabContainer.removeEventListener('TabClose', tabWatcher.onTabClose);
deck.removeEventListener('TabClose', tabWatcher.onTabClose); tabContainer.removeEventListener('TabSelect', tabWatcher.onTabSelect);
deck.removeEventListener('TabSelect', tabWatcher.onTabSelect);
// close extension tabs // Close extension tabs
for ( var tab of win.BrowserApp.tabs ) { for ( var tab of tabBrowser.tabs ) {
var URI = tab.browser.currentURI; var browser = getBrowserForTab(tab);
if ( browser === null ) {
if ( URI.schemeIs('chrome') && URI.host === location.host ) { continue;
win.BrowserApp.closeTab(tab); }
} var URI = browser.currentURI;
if ( URI.schemeIs('chrome') && URI.host === location.host ) {
vAPI.tabs._remove(tab, win);
} }
} }
} }
@ -446,57 +475,84 @@ vAPI.tabs.registerListeners = function() {
vAPI.tabs.getTabId = function(target) { vAPI.tabs.getTabId = function(target) {
if ( vAPI.fennec ) { if ( vAPI.fennec ) {
// Fennec
if ( target.browser ) { if ( target.browser ) {
// target is a tab // target is a tab
return target.id; return target.id;
} }
// target is a browser or contentDocument for ( var win of this.getWindows() ) {
for ( var win of vAPI.tabs.getWindows() ) { var tab = win.BrowserApp.getTabForBrowser(target);
for ( var tab of win.BrowserApp.tabs ) { if ( tab && tab.id ) {
if ( target === tab.browser || return tab.id;
target === tab.window.document ) {
return tab.id;
}
} }
} }
return -1; return -1;
} else {
// desktop Firefox
if ( target.linkedPanel ) {
// target is a tab
return target.linkedPanel;
}
// target is a browser
var i, gBrowser = target.ownerDocument.defaultView.gBrowser;
if ( !gBrowser ) {
return -1;
}
// This should be more efficient from version 35
if ( gBrowser.getTabForBrowser ) {
i = gBrowser.getTabForBrowser(target);
return i ? i.linkedPanel : -1;
}
if ( !gBrowser.browsers ) {
return -1;
}
i = gBrowser.browsers.indexOf(target);
if ( i !== -1 ) {
i = gBrowser.tabs[i].linkedPanel;
}
return i;
} }
if ( target.linkedPanel ) {
// target is a tab
return target.linkedPanel;
}
// target is a browser
var i;
var gBrowser = getOwnerWindow(target).gBrowser;
if ( !gBrowser ) {
return -1;
}
// This should be more efficient from version 35
if ( gBrowser.getTabForBrowser ) {
i = gBrowser.getTabForBrowser(target);
return i ? i.linkedPanel : -1;
}
if ( !gBrowser.browsers ) {
return -1;
}
i = gBrowser.browsers.indexOf(target);
if ( i !== -1 ) {
i = gBrowser.tabs[i].linkedPanel;
}
return i;
};
/******************************************************************************/
// If tabIds is an array, then an array of tabs will be returned,
// otherwise a single tab
vAPI.tabs.getTabsForIds = function(tabIds, tabBrowser) {
var tabId;
var tabs = [];
var singleTab = !Array.isArray(tabIds);
if ( singleTab ) {
tabIds = [tabIds];
}
if ( vAPI.fennec ) {
for ( tabId of tabIds ) {
var tab = tabBrowser.getTabForId(tabId);
if ( tab ) {
tabs.push(tab);
}
}
} else {
var query = [];
for ( tabId of tabIds ) {
query.push('tab[linkedpanel="' + tabId + '"]');
}
query = query.join(',');
tabs = [].slice.call(tabBrowser.tabContainer.querySelectorAll(query));
}
return singleTab ? tabs[0] || null : tabs;
}; };
/******************************************************************************/ /******************************************************************************/
@ -506,36 +562,14 @@ vAPI.tabs.get = function(tabId, callback) {
if ( tabId === null ) { if ( tabId === null ) {
win = Services.wm.getMostRecentWindow('navigator:browser'); win = Services.wm.getMostRecentWindow('navigator:browser');
if ( win.gBrowser ) { tab = getTabBrowser(win).selectedTab;
// desktop Firefox tabId = this.getTabId(tab);
tab = win.gBrowser.selectedTab;
} else if ( win.BrowserApp ) {
// Fennec
tab = win.BrowserApp.selectedTab;
}
tabId = vAPI.tabs.getTabId(tab);
} else { } else {
windows = this.getWindows(); windows = this.getWindows();
for ( win of windows ) {
if ( vAPI.fennec ) { tab = vAPI.tabs.getTabsForIds(tabId, getTabBrowser(win));
// Fennec if ( tab ) {
for ( win of windows ) { break;
tab = win.BrowserApp.getTabForId(tabId);
if ( tab ) {
break;
}
}
} else {
// desktop Firefox
for ( win of windows ) {
tab = win.gBrowser.tabContainer.querySelector(
'tab[linkedpanel="' + tabId + '"]'
);
if ( tab ) {
break;
}
} }
} }
} }
@ -550,63 +584,43 @@ vAPI.tabs.get = function(tabId, callback) {
return; return;
} }
if ( tab.linkedBrowser ) { if ( !windows ) {
// desktop Firefox windows = this.getWindows();
var browser = tab.linkedBrowser;
var gBrowser = win.gBrowser;
if ( !windows ) {
windows = this.getWindows();
}
callback({
id: tabId,
index: gBrowser.browsers.indexOf(browser),
windowId: windows.indexOf(win),
active: tab === gBrowser.selectedTab,
url: browser.currentURI.asciiSpec,
title: tab.label
});
} else if ( tab.browser ) {
// Fennec
var browser = tab.browser;
var BrowserApp = win.BrowserApp;
if ( !windows ) {
windows = this.getWindows();
}
callback({
id: tabId,
index: BrowserApp.tabs.indexOf(tab),
windowId: windows.indexOf(win),
active: tab === BrowserApp.selectedTab,
url: browser.currentURI.asciiSpec,
title: browser.contentDocument.title
});
} }
var browser = getBrowserForTab(tab);
var tabBrowser = getTabBrowser(win);
var tabIndex = vAPI.fennec
? tabBrowser.tabs.indexOf(tab)
: tabBrowser.browsers.indexOf(browser);
callback({
id: tabId,
index: tabIndex,
windowId: windows.indexOf(win),
active: tab === tabBrowser.selectedTab,
url: browser.currentURI.asciiSpec,
title: tab.label
});
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.getAll = function(window) { vAPI.tabs.getAll = function(window) {
var win, tab, tabs = []; var win, tab;
var tabs = [];
for ( win of this.getWindows() ) { for ( win of this.getWindows() ) {
if ( window && window !== win ) { if ( window && window !== win ) {
continue; continue;
} }
var tabList; var tabBrowser = getTabBrowser(win);
if ( win.gBrowser ) { if ( tabBrowser === null ) {
// desktop Firefox continue;
tabList = win.gBrowser.tabs;
} else {
// Fennec
tabList = win.BrowserApp.tabs;
} }
for ( tab of tabList ) { for ( tab of tabBrowser.tabs ) {
tabs.push(tab); tabs.push(tab);
} }
} }
@ -649,34 +663,26 @@ vAPI.tabs.open = function(details) {
details.url = vAPI.getURL(details.url); details.url = vAPI.getURL(details.url);
} }
var tab, tabs; var win, tab, tabBrowser;
if ( details.select ) { if ( details.select ) {
var URI = Services.io.newURI(details.url, null, null); var URI = Services.io.newURI(details.url, null, null);
tabs = this.getAll();
for ( tab of tabs ) { for ( tab of this.getAll() ) {
var browser; var browser = getBrowserForTab(tab);
if ( tab.linkedBrowser ) {
// desktop Firefox
browser = tab.linkedBrowser;
} else {
// Fennec
browser = tab.browser;
}
// Or simply .equals if we care about the fragment // Or simply .equals if we care about the fragment
if ( URI.equalsExceptRef(browser.currentURI) ) { if ( URI.equalsExceptRef(browser.currentURI) === false ) {
var win = browser.ownerDocument.defaultView; continue;
if ( win.gBrowser ) {
// desktop Firefox
win.gBrowser.selectedTab = tab;
} else {
// Fennec
win.BrowserApp.selectTab(tab);
}
return;
} }
tabBrowser = getTabBrowser(getOwnerWindow(tab));
if ( vAPI.fennec ) {
tabBrowser.selectTab(tab);
} else {
tabBrowser.selectedTab = tab;
}
return;
} }
} }
@ -684,53 +690,44 @@ vAPI.tabs.open = function(details) {
details.active = true; details.active = true;
} }
var win = Services.wm.getMostRecentWindow('navigator:browser'); if ( details.tabId ) {
if ( win.gBrowser ) { for ( win in this.getWindows() ) {
// desktop Firefox tab = this.getTabsForIds(details.tabId, win);
var gBrowser = win.gBrowser;
if ( details.index === -1 ) {
details.index = gBrowser.browsers.indexOf(gBrowser.selectedBrowser) + 1;
}
if ( details.tabId ) {
tabs = tabs || this.getAll();
for ( tab of tabs ) {
if ( vAPI.tabs.getTabId(tab) === details.tabId ) {
tab.linkedBrowser.loadURI(details.url);
return;
}
}
}
tab = gBrowser.loadOneTab(details.url, {inBackground: !details.active});
if ( details.index !== undefined ) {
gBrowser.moveTabTo(tab, details.index);
}
} else {
// Fennec
var BrowserApp = win.BrowserApp;
if ( details.index === -1 ) {
details.index = BrowserApp.tabs.indexOf(gBrowser.selectedTab) + 1;
}
if ( details.tabId ) {
tabs = tabs || this.getAll();
tab = BrowserApp.getTabForId(details.tabId);
if ( tab ) { if ( tab ) {
tab.browser.loadURI(details.url); getBrowserForTab(tab).loadURI(details.url);
return; return;
} }
} }
tab = BrowserApp.addTab(details.url, {selected: details.active});
// note that it's impossible to move tabs on Fennec, so don't bother
} }
win = Services.wm.getMostRecentWindow('navigator:browser');
tabBrowser = getTabBrowser(win);
if ( vAPI.fennec ) {
tabBrowser.addTab(details.url, {selected: details.active !== false});
// Note that it's impossible to move tabs on Fennec, so don't bother
return;
}
if ( details.index === -1 ) {
details.index = tabBrowser.browsers.indexOf(tabBrowser.selectedBrowser) + 1;
}
tab = tabBrowser.loadOneTab(details.url, {inBackground: !details.active});
if ( details.index !== undefined ) {
tabBrowser.moveTabTo(tab, details.index);
}
};
/******************************************************************************/
vAPI.tabs._remove = function(tab, tabBrowser) {
if ( vAPI.fennec ) {
tabBrowser.closeTab(tab);
return;
}
tabBrowser.removeTab(tab);
}; };
/******************************************************************************/ /******************************************************************************/
@ -740,35 +737,14 @@ vAPI.tabs.remove = function(tabIds) {
tabIds = [tabIds]; tabIds = [tabIds];
} }
if ( vAPI.fennec ) { for ( var win of this.getWindows() ) {
// Fennec var tabBrowser = getTabBrowser(win);
for ( var win of this.getWindows() ) { var tabs = this.getTabsForIds(tabIds, tabBrowser);
var tabs = win.BrowserApp.tabs; if ( !tabs ) {
if ( !tabs ) { continue;
continue;
}
for ( var tab of tabs ) {
if ( tab.id in tabIds ) {
win.BrowserApp.closeTab(tab);
}
}
} }
} else { for ( var tab of tabs ) {
// desktop Firefox this._remove(tab, tabBrowser);
tabIds = tabIds.map(function(tabId) {
return 'tab[linkedpanel="' + tabId + '"]';
}).join(',');
for ( var win of this.getWindows() ) {
var tabs = win.gBrowser.tabContainer.querySelectorAll(tabIds);
if ( !tabs ) {
continue;
}
for ( var tab of tabs ) {
win.gBrowser.removeTab(tab);
}
} }
} }
}; };
@ -778,21 +754,22 @@ vAPI.tabs.remove = function(tabIds) {
vAPI.tabs.reload = function(tabId) { vAPI.tabs.reload = function(tabId) {
var tab = this.get(tabId); var tab = this.get(tabId);
if ( tab ) { if ( !tab ) {
if ( tab.browser ) { return;
// Fennec
tab.browser.reload();
} else {
// desktop Firefox
tab.ownerDocument.defaultView.gBrowser.reloadTab(tab);
}
} }
if ( vAPI.fennec ) {
tab.browser.reload();
return;
}
getOwnerWindow(tab).gBrowser.reloadTab(tab);
}; };
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.injectScript = function(tabId, details, callback) { vAPI.tabs.injectScript = function(tabId, details, callback) {
var tab = vAPI.tabs.get(tabId); var tab = this.get(tabId);
if ( !tab ) { if ( !tab ) {
return; return;
@ -827,17 +804,8 @@ vAPI.setIcon = function(tabId, iconStatus, badge) {
var win = badge === undefined var win = badge === undefined
? iconStatus ? iconStatus
: Services.wm.getMostRecentWindow('navigator:browser'); : Services.wm.getMostRecentWindow('navigator:browser');
var curTabId = vAPI.tabs.getTabId(getTabBrowser(win).selectedTab);
var tb = vAPI.toolbarButton; var tb = vAPI.toolbarButton;
var selectedTab, curTabId;
if ( win.gBrowser ) {
// desktop Firefox
selectedTab = win.gBrowser.selectedTab;
} else {
// Fennec
selectedTab = win.BrowserApp.selectedTab;
}
curTabId = vAPI.tabs.getTabId(selectedTab);
// from 'TabSelect' event // from 'TabSelect' event
if ( tabId === undefined ) { if ( tabId === undefined ) {
@ -897,9 +865,9 @@ vAPI.messaging.onMessage = function({target, data}) {
if ( !messageManager ) { if ( !messageManager ) {
// Message came from a popup, and its message manager is not usable. // Message came from a popup, and its message manager is not usable.
// So instead we broadcast to the parent window. // So instead we broadcast to the parent window.
messageManager = target messageManager = getOwnerWindow(
.webNavigation.QueryInterface(Ci.nsIDocShell) target.webNavigation.QueryInterface(Ci.nsIDocShell).chromeEventHandler
.chromeEventHandler.ownerDocument.defaultView.messageManager; ).messageManager;
} }
var channelNameRaw = data.channelName; var channelNameRaw = data.channelName;
@ -1430,8 +1398,9 @@ vAPI.toolbarButton = {
/******************************************************************************/ /******************************************************************************/
vAPI.toolbarButton.init = function() { vAPI.toolbarButton.init = function() {
var CustomizableUI;
try { try {
var {CustomizableUI} = Cu.import('resource:///modules/CustomizableUI.jsm', null); CustomizableUI = Cu.import('resource:///modules/CustomizableUI.jsm', null).CustomizableUI;
} catch (ex) { } catch (ex) {
return; return;
} }
@ -1468,6 +1437,7 @@ vAPI.toolbarButton.init = function() {
); );
} else { } else {
this.CUIEvents = {}; this.CUIEvents = {};
this.CUIEvents.onCustomizeEnd = function() { this.CUIEvents.onCustomizeEnd = function() {
var wId = vAPI.toolbarButton.id; var wId = vAPI.toolbarButton.id;
var buttonInPanel = CustomizableUI.getWidget(wId).areaType === CustomizableUI.TYPE_MENU_PANEL; var buttonInPanel = CustomizableUI.getWidget(wId).areaType === CustomizableUI.TYPE_MENU_PANEL;
@ -1489,8 +1459,9 @@ vAPI.toolbarButton.init = function() {
} }
// Anonymous elements need some time to be reachable // Anonymous elements need some time to be reachable
setTimeout(this.updateBadgeStyle, 0); setTimeout(this.updateBadgeStyle, 250);
}.bind(this.CUIEvents); }.bind(this.CUIEvents);
this.CUIEvents.updateBadgeStyle = function() { this.CUIEvents.updateBadgeStyle = function() {
var css = [ var css = [
'background: #666', 'background: #666',
@ -1517,7 +1488,7 @@ vAPI.toolbarButton.init = function() {
this.onCreated = function(button) { this.onCreated = function(button) {
button.setAttribute('badge', ''); button.setAttribute('badge', '');
setTimeout(this.CUIEvents.onCustomizeEnd, 0); setTimeout(this.CUIEvents.onCustomizeEnd, 250);
}; };
CustomizableUI.addListener(this.CUIEvents); CustomizableUI.addListener(this.CUIEvents);
@ -1650,8 +1621,8 @@ vAPI.contextMenu = {
/******************************************************************************/ /******************************************************************************/
vAPI.contextMenu.displayMenuItem = function(e) { vAPI.contextMenu.displayMenuItem = function({target}) {
var doc = e.target.ownerDocument; var doc = target.ownerDocument;
var gContextMenu = doc.defaultView.gContextMenu; var gContextMenu = doc.defaultView.gContextMenu;
if ( !gContextMenu.browser ) { if ( !gContextMenu.browser ) {
@ -1702,28 +1673,25 @@ vAPI.contextMenu.register = function(doc) {
} }
if ( vAPI.fennec ) { if ( vAPI.fennec ) {
// Fennec // TODO https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/NativeWindow/contextmenus/add
// TODO /*var nativeWindow = doc.defaultView.NativeWindow;
/*
var nativeWindow = doc.defaultView.NativeWindow;
contextId = nativeWindow.contextmenus.add( contextId = nativeWindow.contextmenus.add(
this.menuLabel, this.menuLabel,
nativeWindow.contextmenus.linkOpenableContext, // TODO https://developer.mozilla.org/en-US/Add-ons/Firefox_for_Android/API/NativeWindow/contextmenus/add nativeWindow.contextmenus.linkOpenableContext,
this.onCommand); this.onCommand
*/ );*/
} else { return;
// desktop Firefox
var contextMenu = doc.getElementById('contentAreaContextMenu');
var menuitem = doc.createElement('menuitem');
menuitem.setAttribute('id', this.menuItemId);
menuitem.setAttribute('label', this.menuLabel);
menuitem.setAttribute('image', vAPI.getURL('img/browsericons/icon16.svg'));
menuitem.setAttribute('class', 'menuitem-iconic');
menuitem.addEventListener('command', this.onCommand);
contextMenu.addEventListener('popupshowing', this.displayMenuItem);
contextMenu.insertBefore(menuitem, doc.getElementById('inspect-separator'));
} }
var contextMenu = doc.getElementById('contentAreaContextMenu');
var menuitem = doc.createElement('menuitem');
menuitem.setAttribute('id', this.menuItemId);
menuitem.setAttribute('label', this.menuLabel);
menuitem.setAttribute('image', vAPI.getURL('img/browsericons/icon16.svg'));
menuitem.setAttribute('class', 'menuitem-iconic');
menuitem.addEventListener('command', this.onCommand);
contextMenu.addEventListener('popupshowing', this.displayMenuItem);
contextMenu.insertBefore(menuitem, doc.getElementById('inspect-separator'));
}; };
/******************************************************************************/ /******************************************************************************/
@ -1734,17 +1702,15 @@ vAPI.contextMenu.unregister = function(doc) {
} }
if ( vAPI.fennec ) { if ( vAPI.fennec ) {
// Fennec
// TODO // TODO
} else { return;
// desktop Firefox
var menuitem = doc.getElementById(this.menuItemId);
var contextMenu = menuitem.parentNode;
menuitem.removeEventListener('command', this.onCommand);
contextMenu.removeEventListener('popupshowing', this.displayMenuItem);
contextMenu.removeChild(menuitem);
} }
var menuitem = doc.getElementById(this.menuItemId);
var contextMenu = menuitem.parentNode;
menuitem.removeEventListener('command', this.onCommand);
contextMenu.removeEventListener('popupshowing', this.displayMenuItem);
contextMenu.removeChild(menuitem);
}; };
/******************************************************************************/ /******************************************************************************/
@ -1762,13 +1728,14 @@ vAPI.contextMenu.create = function(details, callback) {
} }
this.onCommand = function() { this.onCommand = function() {
var gContextMenu = this.ownerDocument.defaultView.gContextMenu; var gContextMenu = getOwnerWindow(this).gContextMenu;
var details = { var details = {
menuItemId: this.id menuItemId: this.id
}; };
if ( gContextMenu.inFrame ) { if ( gContextMenu.inFrame ) {
details.tagName = 'iframe'; details.tagName = 'iframe';
// Probably won't work with e01s
details.frameUrl = gContextMenu.focusedWindow.location.href; details.frameUrl = gContextMenu.focusedWindow.location.href;
} else if ( gContextMenu.onImage ) { } else if ( gContextMenu.onImage ) {
details.tagName = 'img'; details.tagName = 'img';

View File

@ -59,7 +59,7 @@
vAPI.storage = { vAPI.storage = {
_storage: safari.extension.settings, _storage: safari.extension.settings,
QUOTA_BYTES: 52428800, // copied from Info.plist QUOTA_BYTES: 104857600, // copied from Info.plist
get: function(keys, callback) { get: function(keys, callback) {
if(typeof callback !== 'function') { if(typeof callback !== 'function') {
@ -457,7 +457,7 @@
if(typeof state === "undefined") { if(typeof state === "undefined") {
state = vAPI.tabIconState[tabId] = new TabIconState(); state = vAPI.tabIconState[tabId] = new TabIconState();
} }
icon.badge = state.badge; icon.badge = state.badge;
icon.image = vAPI.getURL("img/browsericons/icon16" + state.img + ".png"); icon.image = vAPI.getURL("img/browsericons/icon16" + state.img + ".png");
}; };
vAPI.setIcon = function(tabId, iconStatus, badge) { vAPI.setIcon = function(tabId, iconStatus, badge) {
@ -475,7 +475,7 @@
vAPI.messaging = { vAPI.messaging = {
listeners: {}, listeners: {},
defaultHandler: null, defaultHandler: null,
NOOPFUNC: function() {}, NOOPFUNC: function() {},
UNHANDLED: 'vAPI.messaging.notHandled' UNHANDLED: 'vAPI.messaging.notHandled'
}; };

View File

@ -1,38 +0,0 @@
#!/bin/bash
#
# This script assumes a linux environment
echo "*** uBlock.fennec: Copying files"
DES=dist/build/uBlock.fennec
rm -rf $DES
mkdir -p $DES
cp -R assets $DES/
rm $DES/assets/*.sh
cp -R src/css $DES/
cp -R src/img $DES/
cp -R src/js $DES/
cp -R src/lib $DES/
cp -R src/_locales $DES/
cp src/*.html $DES/
mv $DES/img/icon_128.png $DES/icon.png
cp platform/firefox/vapi-*.js $DES/js/
cp platform/firefox/bootstrap.js $DES/
cp platform/firefox/frame*.js $DES/
cp platform/firefox/chrome.manifest $DES/
cp platform/firefox/install.rdf $DES/
cp platform/fennec/vapi-*.js $DES/
cp LICENSE.txt $DES/
echo "*** uBlock.fennec: Generating meta..."
python tools/make-firefox-meta.py $DES/
if [ "$1" = all ]; then
echo "*** uBlock.fennec: Creating package..."
pushd $DES/
zip ../uBlock.fennec.xpi -qr *
popd
fi
echo "*** uBlock.fennec: Package done."