1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-16 23:42:39 +01:00

Merge pull request #1324 from chrisaljoudi/NetMonitor_Block_Element

Add a "Block Resource" context menu command to the Firefox Network tab in devtools
This commit is contained in:
Chris Aljoudi 2015-05-14 15:49:55 -06:00
commit 992af976d0
7 changed files with 64 additions and 24 deletions

View File

@ -1943,10 +1943,10 @@ vAPI.contextMenu.displayMenuItem = function({target}) {
/******************************************************************************/ /******************************************************************************/
vAPI.contextMenu.createContextMenuItem = function(doc) { vAPI.contextMenu.createContextMenuItem = function(doc, labelOverride) {
var menuitem = doc.createElement('menuitem'); var menuitem = doc.createElement('menuitem');
menuitem.setAttribute('id', this.menuItemId); menuitem.setAttribute('id', this.menuItemId);
menuitem.setAttribute('label', this.menuLabel); menuitem.setAttribute('label', labelOverride || this.menuLabel);
menuitem.setAttribute('image', vAPI.getURL('img/browsericons/icon16.svg')); menuitem.setAttribute('image', vAPI.getURL('img/browsericons/icon16.svg'));
menuitem.setAttribute('class', 'menuitem-iconic'); menuitem.setAttribute('class', 'menuitem-iconic');
return menuitem; return menuitem;
@ -2018,7 +2018,7 @@ vAPI.contextMenu.registerForWebInspector = function(eventName, toolbox, panel) {
selectedNodeFront = selectedNodeFront.parentNode(); selectedNodeFront = selectedNodeFront.parentNode();
} }
if (selectedNodeFront) { if (selectedNodeFront) {
selectedNodeFront.getUniqueSelector().then(selector => µBlock.elementPickerExec(vAPI.tabs.getTabId(panel.browser), selector)); selectedNodeFront.getUniqueSelector().then(selector => µBlock.elementPickerExec(vAPI.tabs.getTabId(panel.browser), { type: 'element', value: selector}));
// Turn off 3D view, if it's turned on. // Turn off 3D view, if it's turned on.
if (tiltButton && tiltButton.checked) { if (tiltButton && tiltButton.checked) {
@ -2031,6 +2031,24 @@ vAPI.contextMenu.registerForWebInspector = function(eventName, toolbox, panel) {
} }
} }
vAPI.contextMenu.registerForNetMonitor = function(eventName, toolbox, panel) {
var doc = panel.panelWin.document;
var menuPopup = doc.getElementById("network-request-popup");
var insertBeforeMenuItem = doc.getElementById("request-menu-context-separator");
if (menuPopup && insertBeforeMenuItem) {
var menuitem = vAPI.contextMenu.createContextMenuItem(doc, vAPI.i18n('netMonitorContextMenuEntry'));
menuitem.addEventListener('command', function() {
var selectedRequest = panel.panelWin.NetMonitorView.RequestsMenu.selectedAttachment;
if (selectedRequest) {
µBlock.elementPickerExec(vAPI.tabs.getTabId(toolbox.target.tab), { type: 'url', value: selectedRequest.url });
}
});
menuPopup.insertBefore(menuitem, insertBeforeMenuItem);
}
}
/******************************************************************************/ /******************************************************************************/
vAPI.contextMenu.create = function(details, callback) { vAPI.contextMenu.create = function(details, callback) {
@ -2085,6 +2103,7 @@ vAPI.contextMenu.create = function(details, callback) {
if (this.gDevTools) { if (this.gDevTools) {
this.gDevTools.on("inspector-ready", this.registerForWebInspector); this.gDevTools.on("inspector-ready", this.registerForWebInspector);
this.gDevTools.on("netmonitor-ready", this.registerForNetMonitor);
} }
} }
@ -2102,6 +2121,7 @@ vAPI.contextMenu.remove = function() {
if (!vAPI.fennec && this.gDevTools) { if (!vAPI.fennec && this.gDevTools) {
this.gDevTools.off("inspector-ready", this.registerForWebInspector); this.gDevTools.off("inspector-ready", this.registerForWebInspector);
this.gDevTools.off("netmonitor-ready", this.registerForNetMonitor);
} }
this.menuItemId = null; this.menuItemId = null;

View File

@ -147,6 +147,10 @@
"message":"Block element", "message":"Block element",
"description":"English: Block element" "description":"English: Block element"
}, },
"netMonitorContextMenuEntry":{
"message":"Block resource",
"description":"English: Block resource"
},
"settingsCollapseBlockedPrompt":{ "settingsCollapseBlockedPrompt":{
"message":"Hide placeholders of blocked elements", "message":"Hide placeholders of blocked elements",
"description":"English: Hide placeholders of blocked elements" "description":"English: Hide placeholders of blocked elements"

View File

@ -141,7 +141,7 @@ return {
contextMenuClientX: -1, contextMenuClientX: -1,
contextMenuClientY: -1, contextMenuClientY: -1,
epickerTargetElementSelector: null, epickerTarget: null,
epickerEprom: null, epickerEprom: null,
// so that I don't have to care for last comma // so that I don't have to care for last comma

View File

@ -326,13 +326,18 @@ var netFilterFromElement = function(elem, out) {
if ( src.length === 0 ) { if ( src.length === 0 ) {
return; return;
} }
netFilterFromUrl(src, out);
}
var netFilterFromUrl = function(url, out) {
// Remove fragment // Remove fragment
var pos = src.indexOf('#'); var pos = url.indexOf('#');
if ( pos !== -1 ) { if ( pos !== -1 ) {
src = src.slice(0, pos); url = url.slice(0, pos);
} }
var filter = src.replace(/^https?:\/\//, '||'); var filter = url.replace(/^https?:\/\//, '||');
// Anchor absolute filter to hostname // Anchor absolute filter to hostname
out.push(filter); out.push(filter);
@ -344,7 +349,7 @@ var netFilterFromElement = function(elem, out) {
} }
// Suggest a filter which is a result of combining more than one URL. // Suggest a filter which is a result of combining more than one URL.
netFilterFromUnion(src, out); netFilterFromUnion(url, out);
}; };
var netFilterSources = { var netFilterSources = {
@ -472,6 +477,14 @@ var filtersFromElement = function(elem) {
/******************************************************************************/ /******************************************************************************/
var filtersFromUrl = function(url) {
netFilterCandidates.length = 0;
cosmeticFilterCandidates.length = 0;
netFilterFromUrl(url, netFilterCandidates);
};
/******************************************************************************/
var elementsFromFilter = function(filter) { var elementsFromFilter = function(filter) {
var out = []; var out = [];
@ -874,20 +887,23 @@ var startPicker = function(details) {
highlightElements([], true); highlightElements([], true);
var elem = null; // If a target was provided, use it
if (details.target) {
// If a target element was provided, use it if (details.target.type === 'element') {
if (details.targetElementSelector) { filtersFromElement(document.querySelector(details.target.value));
elem = document.querySelector(details.targetElementSelector); } else if (details.target.type === 'url') {
filtersFromUrl(details.target.value);
} else {
console.error('uBlock> unknown element picker target details type: %s', details.target.type);
} }
} else {
// Try using mouse position // Try using mouse position
if (!elem && details.clientX !== -1) { if (details.clientX !== -1) {
elem = elementFromPoint(details.clientX, details.clientY); filtersFromElement(elementFromPoint(details.clientX, details.clientY));
}
} }
if (elem !== null) { if (netFilterCandidates.length > 0 || cosmeticFilterCandidates.length > 0) {
filtersFromElement(elem);
showDialog(); showDialog();
} }
}; };

View File

@ -642,7 +642,7 @@ var onMessage = function(request, sender, callback) {
callback({ callback({
frameContent: this.responseText.replace(reStrings, replacer), frameContent: this.responseText.replace(reStrings, replacer),
targetElementSelector: µb.epickerTargetElementSelector, target: µb.epickerTarget,
clientX: µb.contextMenuClientX, clientX: µb.contextMenuClientX,
clientY: µb.contextMenuClientY, clientY: µb.contextMenuClientY,
eprom: µb.epickerEprom eprom: µb.epickerEprom

View File

@ -287,8 +287,8 @@ var matchWhitelistDirective = function(url, hostname, directive) {
/******************************************************************************/ /******************************************************************************/
µBlock.elementPickerExec = function(tabId, targetElementSelector) { µBlock.elementPickerExec = function(tabId, target) {
this.epickerTargetElementSelector = targetElementSelector; this.epickerTarget = target;
vAPI.tabs.injectScript(tabId, { file: 'js/element-picker.js' }); vAPI.tabs.injectScript(tabId, { file: 'js/element-picker.js' });
}; };

View File

@ -24,7 +24,7 @@ source_locale_dir = pj(build_dir, '_locales')
target_locale_dir = pj(build_dir, 'locale') target_locale_dir = pj(build_dir, 'locale')
language_codes = [] language_codes = []
descriptions = OrderedDict({}) descriptions = OrderedDict({})
title_case_strings = ['pickerContextMenuEntry'] title_case_strings = ['pickerContextMenuEntry', 'netMonitorContextMenuEntry']
for alpha2 in sorted(os.listdir(source_locale_dir)): for alpha2 in sorted(os.listdir(source_locale_dir)):
locale_path = pj(source_locale_dir, alpha2, 'messages.json') locale_path = pj(source_locale_dir, alpha2, 'messages.json')