1
0
mirror of https://github.com/gorhill/uBlock.git synced 2025-01-31 20:21:35 +01:00

Minor code review re. context menu code

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/151

I have been unsuccessful fixing the above issue, but I will
keep the changes made in the process of trying to fix it.
This commit is contained in:
Raymond Hill 2019-07-02 09:43:26 -04:00
parent b122c83aa3
commit 730a83377e
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
3 changed files with 56 additions and 60 deletions

View File

@ -299,9 +299,6 @@ var toChromiumTabId = function(tabId) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.registerListeners = function() { vAPI.tabs.registerListeners = function() {
var onNavigationClient = this.onNavigation || noopFunc;
var onUpdatedClient = this.onUpdated || noopFunc;
// https://developer.chrome.com/extensions/webNavigation // https://developer.chrome.com/extensions/webNavigation
// [onCreatedNavigationTarget ->] // [onCreatedNavigationTarget ->]
// onBeforeNavigate -> // onBeforeNavigate ->
@ -315,74 +312,71 @@ vAPI.tabs.registerListeners = function() {
// properly setup if network requests are fired from within the tab. // properly setup if network requests are fired from within the tab.
// Example: Chromium + case #6 at // Example: Chromium + case #6 at
// http://raymondhill.net/ublock/popup.html // http://raymondhill.net/ublock/popup.html
var reGoodForWebRequestAPI = /^https?:\/\//; const reGoodForWebRequestAPI = /^https?:\/\//;
// https://forums.lanik.us/viewtopic.php?f=62&t=32826 // https://forums.lanik.us/viewtopic.php?f=62&t=32826
// Chromium-based browsers: sanitize target URL. I've seen data: URI with // Chromium-based browsers: sanitize target URL. I've seen data: URI with
// newline characters in standard fields, possibly as a way of evading // newline characters in standard fields, possibly as a way of evading
// filters. As per spec, there should be no whitespaces in a data: URI's // filters. As per spec, there should be no whitespaces in a data: URI's
// standard fields. // standard fields.
var sanitizeURL = function(url) { const sanitizeURL = function(url) {
if ( url.startsWith('data:') === false ) { return url; } if ( url.startsWith('data:') === false ) { return url; }
var pos = url.indexOf(','); const pos = url.indexOf(',');
if ( pos === -1 ) { return url; } if ( pos === -1 ) { return url; }
var s = url.slice(0, pos); const s = url.slice(0, pos);
if ( s.search(/\s/) === -1 ) { return url; } if ( s.search(/\s/) === -1 ) { return url; }
return s.replace(/\s+/, '') + url.slice(pos); return s.replace(/\s+/, '') + url.slice(pos);
}; };
var onCreatedNavigationTarget = function(details) { browser.webNavigation.onCreatedNavigationTarget.addListener(details => {
if ( typeof details.url !== 'string' ) { if ( typeof details.url !== 'string' ) {
details.url = ''; details.url = '';
} }
if ( reGoodForWebRequestAPI.test(details.url) === false ) { if ( reGoodForWebRequestAPI.test(details.url) === false ) {
details.frameId = 0; details.frameId = 0;
details.url = sanitizeURL(details.url); details.url = sanitizeURL(details.url);
onNavigationClient(details); if ( this.onNavigation ) {
this.onNavigation(details);
}
} }
if ( typeof vAPI.tabs.onPopupCreated === 'function' ) { if ( vAPI.tabs.onPopupCreated ) {
vAPI.tabs.onPopupCreated( vAPI.tabs.onPopupCreated(
details.tabId, details.tabId,
details.sourceTabId details.sourceTabId
); );
} }
}; });
var onCommitted = function(details) { browser.webNavigation.onCommitted.addListener(details => {
details.url = sanitizeURL(details.url); details.url = sanitizeURL(details.url);
onNavigationClient(details); if ( this.onNavigation ) {
}; this.onNavigation(details);
var onActivated = function(details) {
if ( vAPI.contextMenu instanceof Object ) {
vAPI.contextMenu.onMustUpdate(details.tabId);
} }
}; });
// https://github.com/gorhill/uBlock/issues/3073 // https://github.com/gorhill/uBlock/issues/3073
// - Fall back to `tab.url` when `changeInfo.url` is not set. // Fall back to `tab.url` when `changeInfo.url` is not set.
var onUpdated = function(tabId, changeInfo, tab) { browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if ( typeof changeInfo.url !== 'string' ) { if ( typeof changeInfo.url !== 'string' ) {
changeInfo.url = tab && tab.url; changeInfo.url = tab && tab.url;
} }
if ( changeInfo.url ) { if ( changeInfo.url ) {
changeInfo.url = sanitizeURL(changeInfo.url); changeInfo.url = sanitizeURL(changeInfo.url);
} }
onUpdatedClient(tabId, changeInfo, tab); if ( this.onUpdated ) {
}; this.onUpdated(tabId, changeInfo, tab);
}
});
chrome.webNavigation.onCommitted.addListener(onCommitted); browser.tabs.onActivated.addListener(( ) => {
// Not supported on Firefox WebExtensions yet. if ( vAPI.contextMenu ) {
if ( chrome.webNavigation.onCreatedNavigationTarget instanceof Object ) { vAPI.contextMenu.onMustUpdate();
chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget); }
} });
chrome.tabs.onActivated.addListener(onActivated);
chrome.tabs.onUpdated.addListener(onUpdated);
if ( typeof this.onClosed === 'function' ) {
chrome.tabs.onRemoved.addListener(this.onClosed);
}
browser.tabs.onRemoved.addListener((tabId, details) => {
this.onClosed(tabId, details);
});
}; };
/******************************************************************************/ /******************************************************************************/
@ -395,7 +389,7 @@ vAPI.tabs.get = function(tabId, callback) {
if ( tabId === null ) { if ( tabId === null ) {
chrome.tabs.query( chrome.tabs.query(
{ active: true, currentWindow: true }, { active: true, currentWindow: true },
function(tabs) { tabs => {
void chrome.runtime.lastError; void chrome.runtime.lastError;
callback( callback(
Array.isArray(tabs) && tabs.length !== 0 ? tabs[0] : null Array.isArray(tabs) && tabs.length !== 0 ? tabs[0] : null

View File

@ -23,7 +23,7 @@
/******************************************************************************/ /******************************************************************************/
µBlock.contextMenu = (function() { µBlock.contextMenu = (( ) => {
/******************************************************************************/ /******************************************************************************/
@ -35,7 +35,7 @@ if ( vAPI.contextMenu === undefined ) {
/******************************************************************************/ /******************************************************************************/
var onBlockElement = function(details, tab) { const onBlockElement = function(details, tab) {
if ( tab === undefined ) { return; } if ( tab === undefined ) { return; }
if ( /^https?:\/\//.test(tab.url) === false ) { return; } if ( /^https?:\/\//.test(tab.url) === false ) { return; }
let tagName = details.tagName || ''; let tagName = details.tagName || '';
@ -62,7 +62,7 @@ var onBlockElement = function(details, tab) {
/******************************************************************************/ /******************************************************************************/
var onTemporarilyAllowLargeMediaElements = function(details, tab) { const onTemporarilyAllowLargeMediaElements = function(details, tab) {
if ( tab === undefined ) { return; } if ( tab === undefined ) { return; }
let pageStore = µBlock.pageStoreFromTabId(tab.id); let pageStore = µBlock.pageStoreFromTabId(tab.id);
if ( pageStore === null ) { return; } if ( pageStore === null ) { return; }
@ -71,7 +71,7 @@ var onTemporarilyAllowLargeMediaElements = function(details, tab) {
/******************************************************************************/ /******************************************************************************/
var onEntryClicked = function(details, tab) { const onEntryClicked = function(details, tab) {
if ( details.menuItemId === 'uBlock0-blockElement' ) { if ( details.menuItemId === 'uBlock0-blockElement' ) {
return onBlockElement(details, tab); return onBlockElement(details, tab);
} }
@ -82,7 +82,7 @@ var onEntryClicked = function(details, tab) {
/******************************************************************************/ /******************************************************************************/
var menuEntries = [ const menuEntries = [
{ {
id: 'uBlock0-blockElement', id: 'uBlock0-blockElement',
title: vAPI.i18n('pickerContextMenuEntry'), title: vAPI.i18n('pickerContextMenuEntry'),
@ -97,9 +97,11 @@ var menuEntries = [
/******************************************************************************/ /******************************************************************************/
var update = function(tabId) { let currentBits = 0;
const update = function(tabId = undefined) {
let newBits = 0; let newBits = 0;
if ( µBlock.userSettings.contextMenuEnabled && tabId !== null ) { if ( µBlock.userSettings.contextMenuEnabled && tabId !== undefined ) {
let pageStore = µBlock.pageStoreFromTabId(tabId); let pageStore = µBlock.pageStoreFromTabId(tabId);
if ( pageStore && pageStore.getNetFilteringSwitch() ) { if ( pageStore && pageStore.getNetFilteringSwitch() ) {
newBits |= 0x01; newBits |= 0x01;
@ -120,26 +122,27 @@ var update = function(tabId) {
vAPI.contextMenu.setEntries(usedEntries, onEntryClicked); vAPI.contextMenu.setEntries(usedEntries, onEntryClicked);
}; };
var currentBits = 0;
vAPI.contextMenu.onMustUpdate = update;
/******************************************************************************/ /******************************************************************************/
return { // https://github.com/uBlockOrigin/uBlock-issues/issues/151
update: function(tabId) { // For unknown reasons, the currently active tab will not be successfully
if ( µBlock.userSettings.contextMenuEnabled && tabId === undefined ) { // looked up after closing a window.
vAPI.tabs.get(null, function(tab) {
if ( tab ) { vAPI.contextMenu.onMustUpdate = function(tabId = undefined) {
update(tab.id); if ( µBlock.userSettings.contextMenuEnabled === false ) {
} return update();
});
return;
}
update(tabId);
} }
if ( tabId !== undefined ) {
return update(tabId);
}
vAPI.tabs.get(null, tab => {
if ( tab instanceof Object === false ) { return; }
update(tab.id);
});
}; };
return { update: vAPI.contextMenu.onMustUpdate };
/******************************************************************************/ /******************************************************************************/
})(); })();

View File

@ -524,10 +524,9 @@ vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
/******************************************************************************/ /******************************************************************************/
vAPI.tabs.onClosed = function(tabId) { vAPI.tabs.onClosed = function(tabId) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) { if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
return;
}
µb.unbindTabFromPageStats(tabId); µb.unbindTabFromPageStats(tabId);
µb.contextMenu.update();
}; };
/******************************************************************************/ /******************************************************************************/