Skip to content

Commit

Permalink
Re-import set-constant scriptlet from upstream
Browse files Browse the repository at this point in the history
Base commit:
- gorhill/uBlock@85cf8f5

Related commit:
- gorhill/uBlock@152cea2
  • Loading branch information
JustOff committed Dec 13, 2020
1 parent dc78de3 commit a1da007
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions assets/resources/resources.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,8 @@ disable-newtab-links.js application/javascript

set-constant.js application/javascript
(function() {
var cValue = '{{2}}';
const thisScript = document.currentScript;
let cValue = '{{2}}';
if ( cValue === 'undefined' ) {
cValue = undefined;
} else if ( cValue === 'false' ) {
Expand All @@ -1799,26 +1800,27 @@ set-constant.js application/javascript
} else {
return;
}
var aborted = false;
var mustAbort = function(v) {
let aborted = false;
const mustAbort = function(v) {
if ( aborted ) { return true; }
aborted =
(v !== undefined && v !== null) &&
(cValue !== undefined && cValue !== null) &&
(typeof v !== typeof cValue);
return aborted;
};
var chain = '{{1}}', owner = window;
var makeProxy = function(owner, chain) {
var desc;
var pos = chain.indexOf('.');
const makeProxy = function(owner, chain) {
const pos = chain.indexOf('.');
if ( pos === -1 ) {
if ( mustAbort(owner[chain]) ) { return; }
desc = Object.getOwnPropertyDescriptor(owner, chain);
const original = owner[chain];
if ( mustAbort(original) ) { return; }
const desc = Object.getOwnPropertyDescriptor(owner, chain);
if ( desc === undefined || desc.get === undefined ) {
Object.defineProperty(owner, chain, {
get: function() {
return cValue;
return document.currentScript === thisScript
? original
: cValue;
},
set: function(a) {
if ( mustAbort(a) ) {
Expand All @@ -1829,18 +1831,18 @@ set-constant.js application/javascript
}
return;
}
var prop = chain.slice(0, pos),
v = owner[prop];
chain = chain.slice(pos + 1);
const prop = chain.slice(0, pos);
let v = owner[prop];
chain = chain.slice(pos + 1);
if (
(v instanceof Object) ||
(typeof v === 'object' && v !== null)
) {
makeProxy(v, chain);
return;
}
desc = Object.getOwnPropertyDescriptor(owner, prop);
if ( desc && desc.set ) { return; }
const desc = Object.getOwnPropertyDescriptor(owner, prop);
if ( desc && desc.set !== undefined ) { return; }
Object.defineProperty(owner, prop, {
get: function() {
return v;
Expand All @@ -1853,7 +1855,7 @@ set-constant.js application/javascript
}
});
};
makeProxy(owner, chain);
makeProxy(window, '{{1}}');
})();


Expand Down

1 comment on commit a1da007

@JustOff
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tag #272.

Please sign in to comment.