Easily extend the rental property based on the previous rental agreement from your activity log
当前为
// ==UserScript==
// @name Easy Property Rent Extension
// @namespace easy.property.rent.extension
// @version v1.0.0
// @description Easily extend the rental property based on the previous rental agreement from your activity log
// @author IceBlueFire [776]
// @license MIT
// @match https://www.torn.com/properties.php*
// @icon https://www.google.com/s2/favicons?sz=64&domain=torn.com
// @grant unsafeWindow
// @grant GM_xmlhttpRequest
// @require https://code.jquery.com/jquery-1.8.2.min.js
// ==/UserScript==
/******************** CONFIG SETTINGS ********************/
const apikey = "#################"; // Full access API key required to pull historical activity log data
const debug = 0;
/****************** END CONFIG SETTINGS *******************/
$(document).ready(function() {
log("Ready");
let debounceTimeout;
function checkTabAndRunScript() {
// Do the stuff!
if (getParam('tab') === 'offerExtension') {
let current_renter = null;
let link = $('.offerExtension-form').find('a.h.t-blue');
if (link.length > 0) {
current_renter = link.attr('href')?.match(/XID=(\d+)/)?.[1] || null;
log("Renter: " + current_renter);
getPreviousValues(current_renter);
} else {
log("No renter found.");
}
} else {
log("Wrong properties tab.");
}
}
function getPreviousValues(current_renter) {
// Look for previous rental agreements and auto-fill input boxes
var duration = 0;
var cost = 0;
const property_id = getParam('ID');
const activity = getRentalHistory();
activity.then(function(result) {
$.each(result.log, function(key, value) {
if(value.data.property_id == property_id && value.data.renter == current_renter) {
duration = value.data.days;
cost = value.data.rent;
return false;
}
});
if(duration != 0) {
// We found a previous rental for this property that matched the current renter
// Target the input fields
let costInput = $('.offerExtension.input-money[data-name="offercost"]');
let daysInput = $('.offerExtension.input-money[data-name="days"]');
// Set the values
costInput.val(cost);
daysInput.val(duration);
// Trigger events to mimic manual input
// costInput.trigger('input').trigger('change').trigger('keyup'); // Attempt to simulate input event for Torn
// daysInput.trigger('input').trigger('change').trigger('keyup'); // Attempt to simulate input event for Torn
$('.offerExtension-form input[type="submit"]').prop('disabled', false);
}
});
}
async function getRentalHistory() {
// Get the activity log for both rental extensions and new rental agreements
return new Promise(resolve => {
const request_url = `https://api.torn.com/user/?selections=log&key=`+apikey+`&log=5943,5937`;
GM_xmlhttpRequest ({
method: "GET",
url: request_url,
headers: {
"Content-Type": "application/json"
},
onload: response => {
try {
const data = JSON.parse(response.responseText);
if(!data) {
log('No response from Torn API');
} else {
return resolve(data)
}
}
catch (e) {
console.error(e);
}
},
onerror: (e) => {
console.error(e);
}
})
});
}
function getParam(name) {
// Check for specific variable in the URL
var results = new RegExp('[\?&]'+ name +'=([^&#]*)').exec(window.location.href);
if (results==null){
return null;
}
else{
return results[1] || 0;
}
}
function log(message) {
if(debug){
console.log("[RentExtension] "+message);
}
}
// Create an observer for the properties page to watch for page changes
// Select the target node
const targetNode = document.getElementById('properties-page-wrap');
if (targetNode) {
// Create a MutationObserver to watch for changes
const observer = new MutationObserver((mutationsList) => {
clearTimeout(debounceTimeout); // Reset the debounce timeout on every change
debounceTimeout = setTimeout(() => {
log("Content changed in #properties-page-wrap");
checkTabAndRunScript(); // Run your script when content settles
}, 500); // Debounce for 500ms
});
// Start observing the target node for configured mutations
observer.observe(targetNode, {
childList: true, // Watch for added/removed child nodes
subtree: true, // Watch the entire subtree of the target node
});
//console.log("MutationObserver is set up with debouncing.");
} else {
console.error("Target node #properties-page-wrap not found.");
}
// Run the script initially in case the page is already on the correct tab
checkTabAndRunScript();
});