From 001094580c0bd31ee007a301792f3e73c0ad48ab Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Sat, 15 May 2021 08:15:57 -0400 Subject: [PATCH] Allow filter list subscription through context menu Related issue/feedback: - https://github.com/uBlockOrigin/uBlock-issues/issues/763#issuecomment-841582704 --- src/_locales/en/messages.json | 4 ++++ src/js/contextmenu.js | 36 +++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index e3b58d437..7af5f7939 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -1043,6 +1043,10 @@ "message": "Block element in frame...", "description": "An entry in the browser's contextual menu" }, + "contextMenuSubscribeToList": { + "message": "Subscribe to filter list...", + "description": "An entry in the browser's contextual menu" + }, "contextMenuTemporarilyAllowLargeMediaElements": { "message": "Temporarily allow large media elements", "description": "A context menu entry, present when large media elements have been blocked on the current site" diff --git a/src/js/contextmenu.js b/src/js/contextmenu.js index 80b2ed7a7..75ec7d36c 100644 --- a/src/js/contextmenu.js +++ b/src/js/contextmenu.js @@ -72,6 +72,32 @@ const onBlockElementInFrame = function(details, tab) { /******************************************************************************/ +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') || '?'; + const hash = µBlock.selectedFilterLists.indexOf(parsedURL) !== -1 + ? '#subscribed' + : ''; + vAPI.tabs.open({ + url: + `/asset-viewer.html` + + `?url=${encodeURIComponent(url)}` + + `&title=${encodeURIComponent(title)}` + + `&subscribe=1${hash}`, + select: true, + }); +}; + +/******************************************************************************/ + const onTemporarilyAllowLargeMediaElements = function(details, tab) { if ( tab === undefined ) { return; } let pageStore = µBlock.pageStoreFromTabId(tab.id); @@ -88,6 +114,9 @@ const onEntryClicked = function(details, tab) { if ( details.menuItemId === 'uBlock0-blockElementInFrame' ) { return onBlockElementInFrame(details, tab); } + if ( details.menuItemId === 'uBlock0-subscribeToList' ) { + return onSubscribeToList(details); + } if ( details.menuItemId === 'uBlock0-temporarilyAllowLargeMediaElements' ) { return onTemporarilyAllowLargeMediaElements(details, tab); } @@ -106,6 +135,12 @@ const menuEntries = { title: vAPI.i18n('contextMenuBlockElementInFrame'), contexts: ['frame'], }, + subscribeToList: { + id: 'uBlock0-subscribeToList', + title: vAPI.i18n('contextMenuSubscribeToList'), + contexts: ['link'], + targetUrlPatterns: [ 'abp:*' ], + }, temporarilyAllowLargeMediaElements: { id: 'uBlock0-temporarilyAllowLargeMediaElements', title: vAPI.i18n('contextMenuTemporarilyAllowLargeMediaElements'), @@ -134,6 +169,7 @@ const update = function(tabId = undefined) { if ( newBits & 0x01 ) { usedEntries.push(menuEntries.blockElement); usedEntries.push(menuEntries.blockElementInFrame); + usedEntries.push(menuEntries.subscribeToList); } if ( newBits & 0x02 ) { usedEntries.push(menuEntries.temporarilyAllowLargeMediaElements);