2014-09-28 18:05:46 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2017-05-27 17:51:24 +02:00
|
|
|
Copyright (C) 2014-2017 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
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
µBlock.contextMenu = (function() {
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
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
|
|
|
var µb = µBlock;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
var onBlockElement = function(details, tab) {
|
2014-09-28 18:05:46 +02:00
|
|
|
if ( tab === undefined ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( /^https?:\/\//.test(tab.url) === false ) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-17 21:44:19 +02:00
|
|
|
var tagName = details.tagName || '';
|
|
|
|
var src = details.frameUrl || details.srcUrl || details.linkUrl || '';
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
µb.elementPickerExec(tab.id, tagName + '\t' + src);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
var onTemporarilyAllowLargeMediaElements = function(details, tab) {
|
2017-09-16 13:59:56 +02:00
|
|
|
if ( tab === undefined ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
var 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
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var onEntryClicked = function(details, tab) {
|
|
|
|
if ( details.menuItemId === 'uBlock0-blockElement' ) {
|
|
|
|
return onBlockElement(details, tab);
|
|
|
|
}
|
|
|
|
if ( details.menuItemId === 'uBlock0-temporarilyAllowLargeMediaElements' ) {
|
|
|
|
return onTemporarilyAllowLargeMediaElements(details, tab);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var menuEntries = [
|
|
|
|
{
|
|
|
|
id: 'uBlock0-blockElement',
|
2014-10-17 21:44:19 +02:00
|
|
|
title: vAPI.i18n('pickerContextMenuEntry'),
|
2016-01-17 19:30:43 +01:00
|
|
|
contexts: ['all'],
|
2014-09-28 18:05:46 +02:00
|
|
|
documentUrlPatterns: ['https://*/*', 'http://*/*']
|
2016-01-17 19:30:43 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'uBlock0-temporarilyAllowLargeMediaElements',
|
|
|
|
title: vAPI.i18n('contextMenuTemporarilyAllowLargeMediaElements'),
|
|
|
|
contexts: ['all'],
|
|
|
|
documentUrlPatterns: ['https://*/*', 'http://*/*']
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var update = function(tabId) {
|
|
|
|
var newBits = 0;
|
|
|
|
if ( µb.userSettings.contextMenuEnabled && tabId !== null ) {
|
|
|
|
var pageStore = µb.pageStoreFromTabId(tabId);
|
|
|
|
if ( pageStore ) {
|
|
|
|
newBits |= 0x01;
|
|
|
|
if ( pageStore.largeMediaCount !== 0 ) {
|
|
|
|
newBits |= 0x02;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( newBits === currentBits ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
currentBits = newBits;
|
|
|
|
var usedEntries = [];
|
|
|
|
if ( newBits & 0x01 ) {
|
|
|
|
usedEntries.push(menuEntries[0]);
|
|
|
|
}
|
|
|
|
if ( newBits & 0x02 ) {
|
|
|
|
usedEntries.push(menuEntries[1]);
|
2014-09-28 18:05:46 +02:00
|
|
|
}
|
2016-01-17 19:30:43 +01:00
|
|
|
vAPI.contextMenu.setEntries(usedEntries, onEntryClicked);
|
2014-09-28 18:05:46 +02:00
|
|
|
};
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
var currentBits = 0;
|
|
|
|
|
|
|
|
vAPI.contextMenu.onMustUpdate = update;
|
|
|
|
|
2014-09-28 18:05:46 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-07-25 01:25:49 +02:00
|
|
|
return {
|
|
|
|
update: function(tabId) {
|
|
|
|
if ( µb.userSettings.contextMenuEnabled && tabId === undefined ) {
|
|
|
|
vAPI.tabs.get(null, function(tab) {
|
|
|
|
if ( tab ) {
|
|
|
|
update(tab.id);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
update(tabId);
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
2014-09-28 18:05:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|