|
26 | 26 | /******************************************************************************/ |
27 | 27 |
|
28 | 28 | µBlock.canUseShortcuts = vAPI.commands instanceof Object; |
29 | | - |
30 | 29 | µBlock.canUpdateShortcuts = µBlock.canUseShortcuts && |
31 | 30 | typeof vAPI.commands.update === 'function'; |
32 | 31 |
|
33 | 32 | /******************************************************************************/ |
34 | 33 |
|
35 | | -(function() { |
36 | | - if ( µBlock.canUseShortcuts === false ) { return; } |
| 34 | +(( ) => { |
37 | 35 |
|
38 | | - vAPI.commands.onCommand.addListener(function(command) { |
39 | | - var µb = µBlock; |
| 36 | +// ***************************************************************************** |
| 37 | +// start of local namespace |
40 | 38 |
|
41 | | - switch ( command ) { |
42 | | - case 'launch-element-zapper': |
43 | | - case 'launch-element-picker': |
44 | | - vAPI.tabs.get(null, function(tab) { |
45 | | - if ( tab instanceof Object === false ) { return; } |
46 | | - µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1; |
47 | | - µb.elementPickerExec(tab.id, undefined, command === 'launch-element-zapper'); |
48 | | - }); |
| 39 | +if ( µBlock.canUseShortcuts === false ) { return; } |
| 40 | + |
| 41 | +const toggleBlockingProfile = function(tab) { |
| 42 | + if ( |
| 43 | + tab instanceof Object === false || |
| 44 | + tab.id <= 0 |
| 45 | + ) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const µb = µBlock; |
| 50 | + const normalURL = µb.normalizePageURL(tab.id, tab.url); |
| 51 | + |
| 52 | + if ( µb.getNetFilteringSwitch(normalURL) === false ) { return; } |
| 53 | + |
| 54 | + const hn = µb.URI.hostnameFromURI(normalURL); |
| 55 | + |
| 56 | + // Construct current blocking profile |
| 57 | + const ssw = µb.sessionSwitches; |
| 58 | + const sfw = µb.sessionFirewall; |
| 59 | + let currentProfile = 0; |
| 60 | + |
| 61 | + if ( ssw.evaluateZ('no-scripting', hn) ) { |
| 62 | + currentProfile |= 0b00000010; |
| 63 | + } |
| 64 | + if ( µb.userSettings.advancedUserEnabled ) { |
| 65 | + if ( sfw.evaluateCellZY(hn, '*', '3p') === 1 ) { |
| 66 | + currentProfile |= 0b00000100; |
| 67 | + } |
| 68 | + if ( sfw.evaluateCellZY(hn, '*', '3p-script') === 1 ) { |
| 69 | + currentProfile |= 0b00001000; |
| 70 | + } |
| 71 | + if ( sfw.evaluateCellZY(hn, '*', '3p-frame') === 1 ) { |
| 72 | + currentProfile |= 0b00010000; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const profiles = []; |
| 77 | + for ( const s of µb.hiddenSettings.blockingProfiles.split(/\s+/) ) { |
| 78 | + const v = parseInt(s, 2); |
| 79 | + if ( isNaN(v) ) { continue; } |
| 80 | + profiles.push(v); |
| 81 | + } |
| 82 | + let newProfile; |
| 83 | + for ( const profile of profiles ) { |
| 84 | + if ( (currentProfile & profile & 0b11111110) !== currentProfile ) { |
| 85 | + newProfile = profile; |
49 | 86 | break; |
50 | | - case 'launch-logger': |
51 | | - vAPI.tabs.get(null, function(tab) { |
52 | | - let hash = tab.url.startsWith(vAPI.getURL('')) ? |
53 | | - '' : |
54 | | - '#_+' + tab.id; |
55 | | - µb.openNewTab({ |
56 | | - url: 'logger-ui.html' + hash, |
57 | | - select: true, |
58 | | - index: -1 |
59 | | - }); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // TODO: Reset to original blocking profile? |
| 91 | + if ( newProfile === undefined ) { return; } |
| 92 | + |
| 93 | + if ( |
| 94 | + (currentProfile & 0b00000010) !== 0 && |
| 95 | + (newProfile & 0b00000010) === 0 |
| 96 | + ) { |
| 97 | + µb.toggleHostnameSwitch({ |
| 98 | + name: 'no-scripting', |
| 99 | + hostname: hn, |
| 100 | + state: false, |
| 101 | + }); |
| 102 | + } |
| 103 | + if ( µb.userSettings.advancedUserEnabled ) { |
| 104 | + if ( |
| 105 | + (currentProfile & 0b00000100) !== 0 && |
| 106 | + (newProfile & 0b00000100) === 0 |
| 107 | + ) { |
| 108 | + µb.toggleFirewallRule({ |
| 109 | + srcHostname: hn, |
| 110 | + desHostname: '*', |
| 111 | + requestType: '3p', |
| 112 | + action: 3, |
| 113 | + }); |
| 114 | + } |
| 115 | + if ( |
| 116 | + (currentProfile & 0b00001000) !== 0 && |
| 117 | + (newProfile & 0b00001000) === 0 |
| 118 | + ) { |
| 119 | + µb.toggleFirewallRule({ |
| 120 | + srcHostname: hn, |
| 121 | + desHostname: '*', |
| 122 | + requestType: '3p-script', |
| 123 | + action: 3, |
| 124 | + }); |
| 125 | + } |
| 126 | + if ( |
| 127 | + (currentProfile & 0b00010000) !== 0 && |
| 128 | + (newProfile & 0b00010000) === 0 |
| 129 | + ) { |
| 130 | + µb.toggleFirewallRule({ |
| 131 | + srcHostname: hn, |
| 132 | + desHostname: '*', |
| 133 | + requestType: '3p-frame', |
| 134 | + action: 3, |
60 | 135 | }); |
61 | | - break; |
62 | | - default: |
63 | | - break; |
64 | 136 | } |
65 | | - }); |
| 137 | + } |
| 138 | + |
| 139 | + if ( newProfile & 0b00000001 ) { |
| 140 | + vAPI.tabs.reload(tab.id); |
| 141 | + } |
| 142 | +}; |
| 143 | + |
| 144 | +vAPI.commands.onCommand.addListener(command => { |
| 145 | + const µb = µBlock; |
| 146 | + |
| 147 | + switch ( command ) { |
| 148 | + case 'launch-element-picker': |
| 149 | + case 'launch-element-zapper': |
| 150 | + vAPI.tabs.get(null, tab => { |
| 151 | + if ( tab instanceof Object === false ) { return; } |
| 152 | + µb.mouseEventRegister.x = µb.mouseEventRegister.y = -1; |
| 153 | + µb.elementPickerExec( |
| 154 | + tab.id, |
| 155 | + undefined, |
| 156 | + command === 'launch-element-zapper' |
| 157 | + ); |
| 158 | + }); |
| 159 | + break; |
| 160 | + case 'launch-logger': |
| 161 | + vAPI.tabs.get(null, tab => { |
| 162 | + const hash = tab.url.startsWith(vAPI.getURL('')) |
| 163 | + ? '' |
| 164 | + : `#_+${tab.id}`; |
| 165 | + µb.openNewTab({ |
| 166 | + url: `logger-ui.html${hash}`, |
| 167 | + select: true, |
| 168 | + index: -1 |
| 169 | + }); |
| 170 | + }); |
| 171 | + break; |
| 172 | + case 'toggle-blocking-profile': |
| 173 | + vAPI.tabs.get(null, toggleBlockingProfile); |
| 174 | + break; |
| 175 | + default: |
| 176 | + break; |
| 177 | + } |
| 178 | +}); |
| 179 | + |
| 180 | +// end of local namespace |
| 181 | +// ***************************************************************************** |
| 182 | + |
66 | 183 | })(); |
67 | 184 |
|
68 | 185 | /******************************************************************************/ |
0 commit comments