話é¡ã®ãã¬ã³ããè¡æã®ã´ã·ãããææ°ã®äºä»¶æ
å ±ããã¡æ©ããå±ãï¼ç¥ããããã¨ãä»ããããããããªãã®å¥½å¥å¿ãæºããæ
å ±ãã
';
entryContent.insertBefore(skeleton, entryContent.firstChild);
window.addEventListener('load', function() {
skeleton.style.transition = 'opacity 0.5s';
skeleton.style.opacity = '0';
setTimeout(function() {
skeleton.style.display = 'none';
}, 500);
});
}
}
// å®å
¨ãªãªã½ã¼ã¹ããªãã¼ã
function safePreloadCriticalResources() {
var criticalImages = Array.from(document.querySelectorAll('.entry-content img')).slice(0, 2).map(function(img) { return img.src || img.dataset.src; });
var commonResources = [
{ href: 'https://cdn.blog.st-hatena.com/css/blog.css', as: 'style' },
{ href: 'https://cdn.blog.st-hatena.com/js/hatenablog.js', as: 'script' }
];
var resources = commonResources.concat(criticalImages.map(function(src) { return { href: src, as: 'image' }; }));
resources.forEach(function(resource) {
if (resource.href) {
var link = document.createElement('link');
link.rel = 'preload';
link.href = resource.href;
link.as = resource.as;
if (resource.as === 'image') {
link.setAttribute('fetchpriority', 'high');
}
document.head.appendChild(link);
}
});
}
// Google AdSense æé©å
function optimizeAdSense() {
function isAdSenseLoaded() {
return typeof adsbygoogle !== 'undefined';
}
function loadAdSense() {
if (!isAdSenseLoaded()) {
var script = document.createElement('script');
script.async = true;
script.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-3827812849061768";
script.crossOrigin = "anonymous";
document.head.appendChild(script);
}
}
if ('IntersectionObserver' in window) {
var observer = new IntersectionObserver(function(entries) {
if (entries[0].isIntersecting) {
loadAdSense();
observer.disconnect();
}
}, {threshold: 0.1});
observer.observe(document.body);
} else {
window.addEventListener('load', function() {
setTimeout(loadAdSense, 3000);
});
}
function observeAds() {
var adObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === 1 && (node.tagName === 'IFRAME' || node.tagName === 'IMG')) {
node.style.maxWidth = '100%';
node.style.height = 'auto';
}
});
}
});
});
document.querySelectorAll('.adsbygoogle').forEach(function(container) {
adObserver.observe(container, { childList: true, subtree: true });
});
}
// åºåã®ä¸ã«ç©ºç½ã追å
function addSpaceBelowAds() {
var adContainers = document.querySelectorAll('p:has(script[src*="adsbygoogle"])');
if (adContainers.length === 0) {
adContainers = Array.from(document.querySelectorAll('p > script[src*="adsbygoogle"]')).map(function(script) {
return script.parentElement;
});
}
adContainers.forEach(function(container) {
container.style.marginBottom = '30px';
});
}
// DOMContentLoadedã¤ãã³ãå¾ã«åºåã®ä¸ã®ç©ºç½ã追å
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', addSpaceBelowAds);
} else {
addSpaceBelowAds();
}
// å®æçã«ç¢ºèªãã¦æ°ãã追å ãããåºåã«ãé©ç¨
setInterval(addSpaceBelowAds, 2000);
window.addEventListener('load', optimizeAdContainers);
window.addEventListener('resize', optimizeAdContainers);
document.addEventListener('DOMContentLoaded', observeAds);
}
// ããã©ã¼ãã³ã¹ã¢ãã¿ãªã³ã°ã®å¼·å
function enhancedPerformanceMonitoring() {
if ('PerformanceObserver' in window) {
new PerformanceObserver(function(list) {
list.getEntries().forEach(function(entry) {
console.log(entry.name + ': ' + entry.startTime + 'ms');
});
}).observe({entryTypes: ['resource', 'paint', 'largest-contentful-paint']});
}
}
// Web Vitals測å®
function measureWebVitals() {
var script = document.createElement('script');
script.src = 'https://unpkg.com/web-vitals/dist/web-vitals.iife.js';
script.onload = function() {
webVitals.getCLS(console.log);
webVitals.getFID(console.log);
webVitals.getLCP(console.log);
};
document.head.appendChild(script);
}
// ã¡ã¤ã³åæåé¢æ°
function initOptimizations() {
createSkeletonUI();
optimizeFonts();
safePreloadCriticalResources();
optimizeImages();
optimizeAdSense();
window.addEventListener('load', function() {
loadCSS('https://fonts.googleapis.com/css2?family=Montserrat:wght@400;700&display=swap');
measureWebVitals();
enhancedPerformanceMonitoring();
});
}
// åæå
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initOptimizations);
} else {
initOptimizations();
}
})();