mirror of
https://github.com/gorhill/uBlock.git
synced 2024-09-03 17:49:39 +02:00
remember/restore element picker session data when same URL
This commit is contained in:
parent
e15001bae1
commit
45882970fa
@ -141,6 +141,9 @@ return {
|
||||
contextMenuClientX: -1,
|
||||
contextMenuClientY: -1,
|
||||
|
||||
epickerTarget: '',
|
||||
epickerEprom: null,
|
||||
|
||||
// so that I don't have to care for last comma
|
||||
dummy: 0
|
||||
};
|
||||
|
@ -144,6 +144,10 @@ var cosmeticFilterCandidates = [];
|
||||
|
||||
var targetElements = [];
|
||||
|
||||
var lastNetFilterSession = window.location.host + window.location.pathname;
|
||||
var lastNetFilterHostname = '';
|
||||
var lastNetFilterUnion = '';
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// For browsers not supporting `:scope`, it's not the end of the world: the
|
||||
@ -234,31 +238,30 @@ var removeElements = function(elems) {
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
var incrementalNetFilter = (function() {
|
||||
var lastHostname = '';
|
||||
var lastNetFilter = '';
|
||||
var netFilterFromUnion = (function() {
|
||||
var reTokenizer = /[^0-9a-z%*]+|[0-9a-z%]+|\*/gi;
|
||||
var a = document.createElement('a');
|
||||
|
||||
var compute = function(to, out) {
|
||||
return function(to, out) {
|
||||
a.href= to;
|
||||
to = a.pathname + a.search;
|
||||
var from = lastNetFilter;
|
||||
var from = lastNetFilterUnion;
|
||||
|
||||
// Reset reference filter when dealing with unrelated URLs
|
||||
if ( from === '' ) {
|
||||
lastHostname = a.host;
|
||||
lastNetFilter = to;
|
||||
return;
|
||||
}
|
||||
if ( a.host !== lastHostname ) {
|
||||
lastHostname = a.host;
|
||||
lastNetFilter = to;
|
||||
if ( from === '' && a.host !== lastNetFilterHostname ) {
|
||||
lastNetFilterHostname = a.host;
|
||||
lastNetFilterUnion = to;
|
||||
localMessager.send({
|
||||
what: 'elementPickerEprom',
|
||||
lastNetFilterSession: lastNetFilterSession,
|
||||
lastNetFilterHostname: lastNetFilterHostname,
|
||||
lastNetFilterUnion: lastNetFilterUnion
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// Related URLs
|
||||
lastHostname = a.host;
|
||||
lastNetFilterHostname = a.host;
|
||||
|
||||
var fromTokens = from.match(reTokenizer);
|
||||
var toTokens = to.match(reTokenizer);
|
||||
@ -287,14 +290,20 @@ var incrementalNetFilter = (function() {
|
||||
}
|
||||
from = fromTokens.join('').replace(/\*\*+/g, '*');
|
||||
if ( from !== '/*' ) {
|
||||
out.push('||' + lastHostname + from);
|
||||
out.push('||' + lastNetFilterHostname + from);
|
||||
} else {
|
||||
from = to;
|
||||
}
|
||||
lastNetFilter = from;
|
||||
};
|
||||
lastNetFilterUnion = from;
|
||||
|
||||
return compute;
|
||||
// Remember across element picker sessions
|
||||
localMessager.send({
|
||||
what: 'elementPickerEprom',
|
||||
lastNetFilterSession: lastNetFilterSession,
|
||||
lastNetFilterHostname: lastNetFilterHostname,
|
||||
lastNetFilterUnion: lastNetFilterUnion
|
||||
});
|
||||
};
|
||||
})();
|
||||
|
||||
/******************************************************************************/
|
||||
@ -334,7 +343,7 @@ var netFilterFromElement = function(elem, out) {
|
||||
}
|
||||
|
||||
// Suggest a filter which is a result of combining more than one URL.
|
||||
incrementalNetFilter(src, out);
|
||||
netFilterFromUnion(src, out);
|
||||
};
|
||||
|
||||
var netFilterSources = {
|
||||
@ -846,6 +855,13 @@ var startPicker = function(details) {
|
||||
pickerRoot.contentWindow.addEventListener('keydown', onKeyPressed, true);
|
||||
pickerRoot.contentWindow.focus();
|
||||
|
||||
// Restore net filter union data if it originate from the same URL.
|
||||
var eprom = details.eprom || {};
|
||||
if ( eprom.lastNetFilterSession === lastNetFilterSession ) {
|
||||
lastNetFilterHostname = eprom.lastNetFilterHostname || '';
|
||||
lastNetFilterUnion = eprom.lastNetFilterUnion || '';
|
||||
}
|
||||
|
||||
// Auto-select a specific target, if any, and if possible
|
||||
|
||||
highlightElements([], true);
|
||||
|
@ -586,7 +586,8 @@ var onMessage = function(request, sender, callback) {
|
||||
frameContent: this.responseText.replace(reStrings, replacer),
|
||||
target: µb.contextMenuTarget,
|
||||
clientX: µb.contextMenuClientX,
|
||||
clientY: µb.contextMenuClientY
|
||||
clientY: µb.contextMenuClientY,
|
||||
eprom: µb.epickerEprom
|
||||
});
|
||||
|
||||
µb.contextMenuTarget = '';
|
||||
@ -595,6 +596,7 @@ var onMessage = function(request, sender, callback) {
|
||||
};
|
||||
xhr.send();
|
||||
return;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -607,6 +609,10 @@ var onMessage = function(request, sender, callback) {
|
||||
µb.appendUserFilters(request.filters);
|
||||
break;
|
||||
|
||||
case 'elementPickerEprom':
|
||||
µb.epickerEprom = request;
|
||||
break;
|
||||
|
||||
default:
|
||||
return vAPI.messaging.UNHANDLED;
|
||||
}
|
||||
|
@ -279,7 +279,7 @@ var matchWhitelistDirective = function(url, hostname, directive) {
|
||||
/******************************************************************************/
|
||||
|
||||
µBlock.elementPickerExec = function(tabId, targetElement) {
|
||||
this.elementPickerTarget = targetElement || '';
|
||||
this.epickerTarget = targetElement || '';
|
||||
vAPI.tabs.injectScript(tabId, { file: 'js/element-picker.js' });
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user