Skip to content

Instantly share code, notes, and snippets.

@harisrab
Created January 6, 2024 11:09
Show Gist options
  • Save harisrab/258ab4c102ef81b8dbcef4b761b019a7 to your computer and use it in GitHub Desktop.
Save harisrab/258ab4c102ef81b8dbcef4b761b019a7 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name intercept_attributed_orders
// @namespace http://tampermonkey.net/
// @version 2024-01-06
// @description try to take over the world!
// @author You
// @match https://app.triplewhale.com/attribution/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=triplewhale.com
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Save the original fetch function in a variable
const originalFetch = window.fetch;
window.attributedOrders = {}
function download(content, fileName, contentType) {
var a = document.createElement("a");
var file = new Blob([content], {type: contentType});
a.href = URL.createObjectURL(file);
a.download = fileName;
a.click();
}
window.fetch = (...args) => {
if (args[0].includes('get-attributed-orders')) {
return originalFetch(...args)
.then(response => {
let duplicate = response.clone();
duplicate.json().then(data => console.log('Fetch Response:', data));
return response;
});
} else {
return originalFetch(...args);
}
};
// Store the original open and send functions of XMLHttpRequest
const originalOpen = XMLHttpRequest.prototype.open;
const originalSend = XMLHttpRequest.prototype.send;
// Override open function to store method and URL in each XMLHttpRequest object
XMLHttpRequest.prototype.open = function(method, url) {
this._method = method;
this._url = url;
originalOpen.apply(this, arguments);
};
// Override send function to handle response
XMLHttpRequest.prototype.send = function (...args) {
if (this._url.includes('get-attributed-orders')) {
this.addEventListener('load', function () {
if (this.readyState == 4 && this.status == 200 && this._url.includes('get-attributed-orders')) {
window.attributedOrders = JSON.parse(this.responseText)
// console.log('XHR Response:', JSON.parse(this.responseText));
let sourceSelector = document.getElementById("att-header-channel-dropdown");
let spanInsideElement = sourceSelector.querySelector("button span.Polaris-Button__Text");
window.buttonTextContent = spanInsideElement.textContent || spanInsideElement.innerText;
console.log('\n')
console.log('Source: ', window.buttonTextContent)
console.log('Campaign Name: ', window.clickedRowContent)
console.log('Attributed Orders: ', window.attributedOrders)
console.log('\n')
console.log('Downloading...')
let jsonString = JSON.stringify(window.attributedOrders);
let fileName = `${window.buttonTextContent.toLowerCase()}___${window.clickedRowContent}.json`;
download(jsonString, fileName, 'application/json');
}
});
}
originalSend.apply(this, args);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment