// --- 以下を</body>の直前に配置 ---
<script>
// --- ULTIMATE SCRIPT for STUDIO ---
// Handles: 1. Parameter Persistence, 2. URL Correction, 3. Form-filling, 4. Read-only lock
(function() {
// --- PART 1: Parameter Storage ---
// On page load, check for URL params and store them in sessionStorage.
try {
const currentParams = new URLSearchParams(window.location.search);
if (currentParams.toString()) {
sessionStorage.setItem('persistentUrlParams', currentParams.toString());
}
} catch (e) {
console.error('Error storing params:', e);
}
// This is the main function that updates the page content.
function runPageUpdateTasks() {
// --- PART 2: URL Correction (The new core logic) ---
// Get stored params and correct the address bar URL if it doesn't match.
const persistentParamsStr = sessionStorage.getItem('persistentUrlParams');
if (persistentParamsStr) {
const currentParamsStr = new URLSearchParams(window.location.search).toString();
if (persistentParamsStr !== currentParamsStr) {
const newUrl = window.location.pathname + '?' + persistentParamsStr + window.location.hash;
// history.replaceState() updates the URL without reloading the page.
history.replaceState(null, '', newUrl);
}
}
// After URL correction, run tasks that depend on the correct URL.
const params = new URLSearchParams(window.location.search);
// --- PART 3: Form Filling & Read-only Lock ---
params.forEach((value, key) => {
const element = document.querySelector(`input[name="${key}"], textarea[name="${key}"]`);
if (element) {
element.value = value;
element.readOnly = true;
element.setAttribute('readonly', 'true'); // Failsafe
}
});
// --- PART 4: Link Rewriting ---
// Rewrites all internal links on the page for the *next* click.
const siteDomain = window.location.hostname;
const allLinks = document.querySelectorAll('a[href]');
allLinks.forEach(link => {
if (link.dataset.paramsAppended) return;
try {
const linkUrl = new URL(link.href);
if (linkUrl.hostname === siteDomain || !linkUrl.hostname) {
const newParams = new URLSearchParams(params.toString());
newParams.forEach((value, key) => {
linkUrl.searchParams.set(key, value);
});
link.href = linkUrl.toString();
link.dataset.paramsAppended = 'true';
}
} catch (e) { /* Ignore invalid links like mailto: */ }
});
}
// --- Main Execution Logic ---
// This robust runner ensures the tasks run at the right time.
function initialize() {
runPageUpdateTasks();
// Use MutationObserver to re-run tasks if STUDIO dynamically changes the page.
const observer = new MutationObserver(runPageUpdateTasks);
observer.observe(document.body, { childList: true, subtree: true, attributes: false });
}
if (document.readyState === 'interactive' || document.readyState === 'complete') {
initialize();
} else {
window.addEventListener('DOMContentLoaded', initialize);
}
})();
</script>