This commit is contained in:
SukkaBot
2023-12-19 14:59:27 +00:00
parent 399ac39a20
commit 89958bd640
8 changed files with 843 additions and 474 deletions

View File

@@ -1,89 +1,124 @@
(function () {
'use strict';
// https://developers.google.com/analytics/devguides/collection/analyticsjs/
const noopfn = function () {
};
//
const Tracker = function () {
};
const p = Tracker.prototype;
p.get = noopfn;
p.set = noopfn;
p.send = noopfn;
//
const w = window;
const gaName = w.GoogleAnalyticsObject || 'ga';
const gaQueue = w[gaName];
const ga = function (...args) {
const len = args.length;
if (len === 0) { return; }
let fn;
const a = args[len - 1];
if (typeof a === 'object' && typeof a.hitCallback === 'function') {
fn = a.hitCallback;
} else if (typeof a === 'function') {
fn = () => { a(ga.create()); };
} else {
const pos = args.indexOf('hitCallback');
if (pos !== -1 && typeof args[pos + 1] === 'function') {
fn = args[pos + 1];
}
(function(source, args) {
function GoogleAnalytics(source) {
var Tracker = function Tracker() {};
var proto = Tracker.prototype;
proto.get = noopFunc;
proto.set = noopFunc;
proto.send = noopFunc;
var googleAnalyticsName = window.GoogleAnalyticsObject || "ga";
function ga(a) {
var len = arguments.length;
if (len === 0) {
return;
}
var lastArg = arguments[len - 1];
var replacer;
if (lastArg instanceof Object && lastArg !== null && typeof lastArg.hitCallback === "function") {
replacer = lastArg.hitCallback;
} else if (typeof lastArg === "function") {
replacer = function replacer() {
lastArg(ga.create());
};
}
try {
setTimeout(replacer, 1);
} catch (ex) {}
}
ga.create = function() {
return new Tracker;
};
ga.getByName = function() {
return new Tracker;
};
ga.getAll = function() {
return [ new Tracker ];
};
ga.remove = noopFunc;
ga.loaded = true;
window[googleAnalyticsName] = ga;
var _window = window, dataLayer = _window.dataLayer, google_optimize = _window.google_optimize;
if (dataLayer instanceof Object === false) {
return;
}
if (dataLayer.hide instanceof Object && typeof dataLayer.hide.end === "function") {
dataLayer.hide.end();
}
var handleCallback = function handleCallback(dataObj, funcName) {
if (dataObj && typeof dataObj[funcName] === "function") {
setTimeout(dataObj[funcName]);
}
};
if (typeof dataLayer.push === "function") {
dataLayer.push = function(data) {
if (data instanceof Object) {
handleCallback(data, "eventCallback");
for (var key in data) {
handleCallback(data[key], "event_callback");
}
if (!data.hasOwnProperty("eventCallback") && !data.hasOwnProperty("eventCallback")) {
[].push.call(window.dataLayer, data);
}
}
if (Array.isArray(data)) {
data.forEach((function(arg) {
handleCallback(arg, "callback");
}));
}
return noopFunc;
};
}
if (google_optimize instanceof Object && typeof google_optimize.get === "function") {
var googleOptimizeWrapper = {
get: noopFunc
};
window.google_optimize = googleOptimizeWrapper;
}
hit(source);
}
if (typeof fn !== 'function') { return; }
function hit(source) {
if (source.verbose !== true) {
return;
}
try {
var log = console.log.bind(console);
var trace = console.trace.bind(console);
var prefix = source.ruleText || "";
if (source.domainName) {
var AG_SCRIPTLET_MARKER = "#%#//";
var UBO_SCRIPTLET_MARKER = "##+js";
var ruleStartIndex;
if (source.ruleText.includes(AG_SCRIPTLET_MARKER)) {
ruleStartIndex = source.ruleText.indexOf(AG_SCRIPTLET_MARKER);
} else if (source.ruleText.includes(UBO_SCRIPTLET_MARKER)) {
ruleStartIndex = source.ruleText.indexOf(UBO_SCRIPTLET_MARKER);
}
var rulePart = source.ruleText.slice(ruleStartIndex);
prefix = "".concat(source.domainName).concat(rulePart);
}
log("".concat(prefix, " trace start"));
if (trace) {
trace();
}
log("".concat(prefix, " trace end"));
} catch (e) {}
if (typeof window.__debug === "function") {
window.__debug(source);
}
}
function noopFunc() {}
function noopNull() {
return null;
}
function noopArray() {
return [];
}
const updatedArgs = args ? [].concat(source).concat(args) : [ source ];
try {
fn();
} catch {
GoogleAnalytics.apply(this, updatedArgs);
} catch (e) {
console.log(e);
}
};
ga.create = function () {
return new Tracker();
};
ga.getByName = function () {
return new Tracker();
};
ga.getAll = function () {
return [new Tracker()];
};
ga.remove = noopfn;
// https://github.com/uBlockOrigin/uAssets/issues/2107
ga.loaded = true;
w[gaName] = ga;
// https://github.com/gorhill/uBlock/issues/3075
const dl = w.dataLayer;
if (typeof dl === 'object') {
if (typeof dl.hide === 'object' && typeof dl.hide.end === 'function') {
dl.hide.end();
dl.hide.end = () => { };
}
if (typeof dl.push === 'function') {
const doCallback = function (item) {
if (typeof item === 'object' === false) { return; }
if (typeof item.eventCallback !== 'function') { return; }
// eslint-disable-next-line sukka/prefer-timer-id -- deliberate use of setTimeout
setTimeout(item.eventCallback, 1);
item.eventCallback = () => { };
};
dl.push = new Proxy(dl.push, {
apply(target, thisArg, args) {
doCallback(args[0]);
return Reflect.apply(target, thisArg, args);
}
});
if (Array.isArray(dl)) {
const q = dl.slice();
for (const item of q) {
doCallback(item);
}
}
}
}
// empty ga queue
if (typeof gaQueue === 'function' && Array.isArray(gaQueue.q)) {
const q = gaQueue.q.slice();
gaQueue.q.length = 0;
for (const entry of q) {
ga(...entry);
}
}
}());
})({
name: "google-analytics",
args: []
}, []);