2014-09-28 18:05:46 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-09-07 16:42:59 +02:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-09-28 18:05:46 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2017-05-27 17:51:24 +02:00
|
|
|
'use strict';
|
|
|
|
|
2014-09-28 18:05:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
import µb from './background.js';
|
2022-09-13 23:44:24 +02:00
|
|
|
import { i18n$ } from './i18n.js';
|
2021-07-25 16:55:35 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
const contextMenu = (( ) => {
|
2014-09-28 18:05:46 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-07-24 17:35:22 +02:00
|
|
|
if ( vAPI.contextMenu === undefined ) {
|
2017-07-25 01:25:49 +02:00
|
|
|
return {
|
|
|
|
update: function() {}
|
|
|
|
};
|
2017-07-24 17:35:22 +02:00
|
|
|
}
|
|
|
|
|
2014-09-28 18:05:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-02 15:43:26 +02:00
|
|
|
const onBlockElement = function(details, tab) {
|
2018-09-07 16:42:59 +02:00
|
|
|
if ( tab === undefined ) { return; }
|
|
|
|
if ( /^https?:\/\//.test(tab.url) === false ) { return; }
|
|
|
|
let tagName = details.tagName || '';
|
|
|
|
let src = details.frameUrl || details.srcUrl || details.linkUrl || '';
|
2014-10-17 21:44:19 +02:00
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( !tagName ) {
|
2014-10-17 21:44:19 +02:00
|
|
|
if ( typeof details.frameUrl === 'string' ) {
|
|
|
|
tagName = 'iframe';
|
|
|
|
} else if ( typeof details.srcUrl === 'string' ) {
|
|
|
|
if ( details.mediaType === 'image' ) {
|
|
|
|
tagName = 'img';
|
|
|
|
} else if ( details.mediaType === 'video' ) {
|
|
|
|
tagName = 'video';
|
|
|
|
} else if ( details.mediaType === 'audio' ) {
|
|
|
|
tagName = 'audio';
|
|
|
|
}
|
|
|
|
} else if ( typeof details.linkUrl === 'string' ) {
|
|
|
|
tagName = 'a';
|
2014-09-28 18:05:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.epickerArgs.mouse = true;
|
|
|
|
µb.elementPickerExec(tab.id, 0, `${tagName}\t${src}`);
|
2020-12-05 21:26:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
const onBlockElementInFrame = function(details, tab) {
|
|
|
|
if ( tab === undefined ) { return; }
|
|
|
|
if ( /^https?:\/\//.test(details.frameUrl) === false ) { return; }
|
2021-07-29 01:48:38 +02:00
|
|
|
µb.epickerArgs.mouse = false;
|
|
|
|
µb.elementPickerExec(tab.id, details.frameId);
|
2014-09-28 18:05:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-05-15 14:15:57 +02:00
|
|
|
const onSubscribeToList = function(details) {
|
|
|
|
let parsedURL;
|
|
|
|
try {
|
|
|
|
parsedURL = new URL(details.linkUrl);
|
|
|
|
}
|
|
|
|
catch(ex) {
|
|
|
|
}
|
|
|
|
if ( parsedURL instanceof URL === false ) { return; }
|
|
|
|
const url = parsedURL.searchParams.get('location');
|
|
|
|
if ( url === null ) { return; }
|
|
|
|
const title = parsedURL.searchParams.get('title') || '?';
|
2021-07-29 01:48:38 +02:00
|
|
|
const hash = µb.selectedFilterLists.indexOf(parsedURL) !== -1
|
2021-05-15 14:15:57 +02:00
|
|
|
? '#subscribed'
|
|
|
|
: '';
|
|
|
|
vAPI.tabs.open({
|
|
|
|
url:
|
|
|
|
`/asset-viewer.html` +
|
|
|
|
`?url=${encodeURIComponent(url)}` +
|
|
|
|
`&title=${encodeURIComponent(title)}` +
|
|
|
|
`&subscribe=1${hash}`,
|
|
|
|
select: true,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-02 15:43:26 +02:00
|
|
|
const onTemporarilyAllowLargeMediaElements = function(details, tab) {
|
2017-09-16 13:59:56 +02:00
|
|
|
if ( tab === undefined ) { return; }
|
2021-07-29 01:48:38 +02:00
|
|
|
const pageStore = µb.pageStoreFromTabId(tab.id);
|
2017-09-16 13:59:56 +02:00
|
|
|
if ( pageStore === null ) { return; }
|
|
|
|
pageStore.temporarilyAllowLargeMediaElements(true);
|
2016-01-17 19:30:43 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-02 15:43:26 +02:00
|
|
|
const onEntryClicked = function(details, tab) {
|
2016-01-17 19:30:43 +01:00
|
|
|
if ( details.menuItemId === 'uBlock0-blockElement' ) {
|
|
|
|
return onBlockElement(details, tab);
|
|
|
|
}
|
2020-12-05 21:26:29 +01:00
|
|
|
if ( details.menuItemId === 'uBlock0-blockElementInFrame' ) {
|
|
|
|
return onBlockElementInFrame(details, tab);
|
|
|
|
}
|
2021-07-12 17:55:58 +02:00
|
|
|
if ( details.menuItemId === 'uBlock0-blockResource' ) {
|
|
|
|
return onBlockElement(details, tab);
|
|
|
|
}
|
2021-05-15 14:15:57 +02:00
|
|
|
if ( details.menuItemId === 'uBlock0-subscribeToList' ) {
|
|
|
|
return onSubscribeToList(details);
|
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
if ( details.menuItemId === 'uBlock0-temporarilyAllowLargeMediaElements' ) {
|
|
|
|
return onTemporarilyAllowLargeMediaElements(details, tab);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-12-05 21:26:29 +01:00
|
|
|
const menuEntries = {
|
|
|
|
blockElement: {
|
2016-01-17 19:30:43 +01:00
|
|
|
id: 'uBlock0-blockElement',
|
2022-09-13 23:44:24 +02:00
|
|
|
title: i18n$('pickerContextMenuEntry'),
|
2021-07-12 17:55:58 +02:00
|
|
|
contexts: [ 'all' ],
|
2016-01-17 19:30:43 +01:00
|
|
|
},
|
2020-12-05 21:26:29 +01:00
|
|
|
blockElementInFrame: {
|
|
|
|
id: 'uBlock0-blockElementInFrame',
|
2022-09-13 23:44:24 +02:00
|
|
|
title: i18n$('contextMenuBlockElementInFrame'),
|
2021-07-12 17:55:58 +02:00
|
|
|
contexts: [ 'frame' ],
|
|
|
|
},
|
|
|
|
blockResource: {
|
|
|
|
id: 'uBlock0-blockResource',
|
2022-09-13 23:44:24 +02:00
|
|
|
title: i18n$('pickerContextMenuEntry'),
|
2021-07-12 17:55:58 +02:00
|
|
|
contexts: [ 'audio', 'frame', 'image', 'video' ],
|
2020-12-05 21:26:29 +01:00
|
|
|
},
|
2021-05-15 14:15:57 +02:00
|
|
|
subscribeToList: {
|
|
|
|
id: 'uBlock0-subscribeToList',
|
2022-09-13 23:44:24 +02:00
|
|
|
title: i18n$('contextMenuSubscribeToList'),
|
2021-07-12 17:55:58 +02:00
|
|
|
contexts: [ 'link' ],
|
2022-02-16 21:17:22 +01:00
|
|
|
targetUrlPatterns: [ 'abp:*', 'https://subscribe.adblockplus.org/*' ],
|
2021-05-15 14:15:57 +02:00
|
|
|
},
|
2020-12-05 21:26:29 +01:00
|
|
|
temporarilyAllowLargeMediaElements: {
|
2016-01-17 19:30:43 +01:00
|
|
|
id: 'uBlock0-temporarilyAllowLargeMediaElements',
|
2022-09-13 23:44:24 +02:00
|
|
|
title: i18n$('contextMenuTemporarilyAllowLargeMediaElements'),
|
2021-07-12 17:55:58 +02:00
|
|
|
contexts: [ 'all' ],
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
2020-12-05 21:26:29 +01:00
|
|
|
};
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-02 15:43:26 +02:00
|
|
|
let currentBits = 0;
|
|
|
|
|
|
|
|
const update = function(tabId = undefined) {
|
2018-09-07 16:42:59 +02:00
|
|
|
let newBits = 0;
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( µb.userSettings.contextMenuEnabled && tabId !== undefined ) {
|
|
|
|
const pageStore = µb.pageStoreFromTabId(tabId);
|
2018-09-07 16:42:59 +02:00
|
|
|
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
|
2021-07-12 17:55:58 +02:00
|
|
|
if ( pageStore.shouldApplySpecificCosmeticFilters(0) ) {
|
|
|
|
newBits |= 0b0001;
|
|
|
|
} else {
|
|
|
|
newBits |= 0b0010;
|
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
if ( pageStore.largeMediaCount !== 0 ) {
|
2021-07-12 17:55:58 +02:00
|
|
|
newBits |= 0b0100;
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-12 17:55:58 +02:00
|
|
|
newBits |= 0b1000;
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
2018-09-07 16:42:59 +02:00
|
|
|
if ( newBits === currentBits ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
currentBits = newBits;
|
2021-07-12 17:55:58 +02:00
|
|
|
const usedEntries = [];
|
|
|
|
if ( newBits & 0b0001 ) {
|
2020-12-05 21:26:29 +01:00
|
|
|
usedEntries.push(menuEntries.blockElement);
|
|
|
|
usedEntries.push(menuEntries.blockElementInFrame);
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
2021-07-12 17:55:58 +02:00
|
|
|
if ( newBits & 0b0010 ) {
|
|
|
|
usedEntries.push(menuEntries.blockResource);
|
|
|
|
}
|
|
|
|
if ( newBits & 0b0100 ) {
|
2020-12-05 21:26:29 +01:00
|
|
|
usedEntries.push(menuEntries.temporarilyAllowLargeMediaElements);
|
2014-09-28 18:05:46 +02:00
|
|
|
}
|
2021-07-12 17:55:58 +02:00
|
|
|
if ( newBits & 0b1000 ) {
|
|
|
|
usedEntries.push(menuEntries.subscribeToList);
|
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
vAPI.contextMenu.setEntries(usedEntries, onEntryClicked);
|
2014-09-28 18:05:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-07-02 15:43:26 +02:00
|
|
|
// 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.
|
|
|
|
|
2019-09-16 15:45:17 +02:00
|
|
|
vAPI.contextMenu.onMustUpdate = async function(tabId = undefined) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( µb.userSettings.contextMenuEnabled === false ) {
|
2019-07-02 15:43:26 +02:00
|
|
|
return update();
|
|
|
|
}
|
|
|
|
if ( tabId !== undefined ) {
|
|
|
|
return update(tabId);
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
2019-09-16 15:45:17 +02:00
|
|
|
const tab = await vAPI.tabs.getCurrent();
|
|
|
|
if ( tab instanceof Object === false ) { return; }
|
|
|
|
update(tab.id);
|
2014-09-28 18:05:46 +02:00
|
|
|
};
|
|
|
|
|
2019-07-02 15:43:26 +02:00
|
|
|
return { update: vAPI.contextMenu.onMustUpdate };
|
|
|
|
|
2014-09-28 18:05:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
2021-07-29 01:48:38 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
export default contextMenu;
|
|
|
|
|
|
|
|
/******************************************************************************/
|