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:
parent
b122c83aa3
commit
730a83377e
@ -299,9 +299,6 @@ var toChromiumTabId = function(tabId) {
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.tabs.registerListeners = function() {
|
||||
var onNavigationClient = this.onNavigation || noopFunc;
|
||||
var onUpdatedClient = this.onUpdated || noopFunc;
|
||||
|
||||
// https://developer.chrome.com/extensions/webNavigation
|
||||
// [onCreatedNavigationTarget ->]
|
||||
// onBeforeNavigate ->
|
||||
@ -315,74 +312,71 @@ vAPI.tabs.registerListeners = function() {
|
||||
// properly setup if network requests are fired from within the tab.
|
||||
// Example: Chromium + case #6 at
|
||||
// http://raymondhill.net/ublock/popup.html
|
||||
var reGoodForWebRequestAPI = /^https?:\/\//;
|
||||
const reGoodForWebRequestAPI = /^https?:\/\//;
|
||||
|
||||
// https://forums.lanik.us/viewtopic.php?f=62&t=32826
|
||||
// Chromium-based browsers: sanitize target URL. I've seen data: URI with
|
||||
// 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
|
||||
// standard fields.
|
||||
var sanitizeURL = function(url) {
|
||||
const sanitizeURL = function(url) {
|
||||
if ( url.startsWith('data:') === false ) { return url; }
|
||||
var pos = url.indexOf(',');
|
||||
const pos = url.indexOf(',');
|
||||
if ( pos === -1 ) { return url; }
|
||||
var s = url.slice(0, pos);
|
||||
const s = url.slice(0, pos);
|
||||
if ( s.search(/\s/) === -1 ) { return url; }
|
||||
return s.replace(/\s+/, '') + url.slice(pos);
|
||||
};
|
||||
|
||||
var onCreatedNavigationTarget = function(details) {
|
||||
browser.webNavigation.onCreatedNavigationTarget.addListener(details => {
|
||||
if ( typeof details.url !== 'string' ) {
|
||||
details.url = '';
|
||||
}
|
||||
if ( reGoodForWebRequestAPI.test(details.url) === false ) {
|
||||
details.frameId = 0;
|
||||
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(
|
||||
details.tabId,
|
||||
details.sourceTabId
|
||||
);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
var onCommitted = function(details) {
|
||||
browser.webNavigation.onCommitted.addListener(details => {
|
||||
details.url = sanitizeURL(details.url);
|
||||
onNavigationClient(details);
|
||||
};
|
||||
|
||||
var onActivated = function(details) {
|
||||
if ( vAPI.contextMenu instanceof Object ) {
|
||||
vAPI.contextMenu.onMustUpdate(details.tabId);
|
||||
if ( this.onNavigation ) {
|
||||
this.onNavigation(details);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
// https://github.com/gorhill/uBlock/issues/3073
|
||||
// - Fall back to `tab.url` when `changeInfo.url` is not set.
|
||||
var onUpdated = function(tabId, changeInfo, tab) {
|
||||
// Fall back to `tab.url` when `changeInfo.url` is not set.
|
||||
browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
|
||||
if ( typeof changeInfo.url !== 'string' ) {
|
||||
changeInfo.url = tab && tab.url;
|
||||
}
|
||||
if ( changeInfo.url ) {
|
||||
changeInfo.url = sanitizeURL(changeInfo.url);
|
||||
}
|
||||
onUpdatedClient(tabId, changeInfo, tab);
|
||||
};
|
||||
|
||||
chrome.webNavigation.onCommitted.addListener(onCommitted);
|
||||
// Not supported on Firefox WebExtensions yet.
|
||||
if ( chrome.webNavigation.onCreatedNavigationTarget instanceof Object ) {
|
||||
chrome.webNavigation.onCreatedNavigationTarget.addListener(onCreatedNavigationTarget);
|
||||
if ( this.onUpdated ) {
|
||||
this.onUpdated(tabId, changeInfo, tab);
|
||||
}
|
||||
chrome.tabs.onActivated.addListener(onActivated);
|
||||
chrome.tabs.onUpdated.addListener(onUpdated);
|
||||
});
|
||||
|
||||
if ( typeof this.onClosed === 'function' ) {
|
||||
chrome.tabs.onRemoved.addListener(this.onClosed);
|
||||
browser.tabs.onActivated.addListener(( ) => {
|
||||
if ( vAPI.contextMenu ) {
|
||||
vAPI.contextMenu.onMustUpdate();
|
||||
}
|
||||
});
|
||||
|
||||
browser.tabs.onRemoved.addListener((tabId, details) => {
|
||||
this.onClosed(tabId, details);
|
||||
});
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
@ -395,7 +389,7 @@ vAPI.tabs.get = function(tabId, callback) {
|
||||
if ( tabId === null ) {
|
||||
chrome.tabs.query(
|
||||
{ active: true, currentWindow: true },
|
||||
function(tabs) {
|
||||
tabs => {
|
||||
void chrome.runtime.lastError;
|
||||
callback(
|
||||
Array.isArray(tabs) && tabs.length !== 0 ? tabs[0] : null
|
||||
|
@ -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 ( /^https?:\/\//.test(tab.url) === false ) { return; }
|
||||
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; }
|
||||
let pageStore = µBlock.pageStoreFromTabId(tab.id);
|
||||
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' ) {
|
||||
return onBlockElement(details, tab);
|
||||
}
|
||||
@ -82,7 +82,7 @@ var onEntryClicked = function(details, tab) {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var menuEntries = [
|
||||
const menuEntries = [
|
||||
{
|
||||
id: 'uBlock0-blockElement',
|
||||
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;
|
||||
if ( µBlock.userSettings.contextMenuEnabled && tabId !== null ) {
|
||||
if ( µBlock.userSettings.contextMenuEnabled && tabId !== undefined ) {
|
||||
let pageStore = µBlock.pageStoreFromTabId(tabId);
|
||||
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
|
||||
newBits |= 0x01;
|
||||
@ -120,26 +122,27 @@ var update = function(tabId) {
|
||||
vAPI.contextMenu.setEntries(usedEntries, onEntryClicked);
|
||||
};
|
||||
|
||||
var currentBits = 0;
|
||||
|
||||
vAPI.contextMenu.onMustUpdate = update;
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
return {
|
||||
update: function(tabId) {
|
||||
if ( µBlock.userSettings.contextMenuEnabled && tabId === undefined ) {
|
||||
vAPI.tabs.get(null, function(tab) {
|
||||
if ( tab ) {
|
||||
// https://github.com/uBlockOrigin/uBlock-issues/issues/151
|
||||
// For unknown reasons, the currently active tab will not be successfully
|
||||
// looked up after closing a window.
|
||||
|
||||
vAPI.contextMenu.onMustUpdate = function(tabId = undefined) {
|
||||
if ( µBlock.userSettings.contextMenuEnabled === false ) {
|
||||
return update();
|
||||
}
|
||||
if ( tabId !== undefined ) {
|
||||
return update(tabId);
|
||||
}
|
||||
vAPI.tabs.get(null, tab => {
|
||||
if ( tab instanceof Object === false ) { return; }
|
||||
update(tab.id);
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
update(tabId);
|
||||
}
|
||||
};
|
||||
|
||||
return { update: vAPI.contextMenu.onMustUpdate };
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})();
|
||||
|
@ -524,10 +524,9 @@ vAPI.tabs.onUpdated = function(tabId, changeInfo, tab) {
|
||||
/******************************************************************************/
|
||||
|
||||
vAPI.tabs.onClosed = function(tabId) {
|
||||
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
|
||||
return;
|
||||
}
|
||||
if ( vAPI.isBehindTheSceneTabId(tabId) ) { return; }
|
||||
µb.unbindTabFromPageStats(tabId);
|
||||
µb.contextMenu.update();
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
Loading…
x
Reference in New Issue
Block a user