-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (123 loc) · 4.67 KB
/
script.js
File metadata and controls
140 lines (123 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
(function () {
var CTA_ID = 'sgai-cta-widget';
function buildCta() {
var el = document.createElement('div');
el.id = CTA_ID;
el.className = 'sgai-cta';
el.innerHTML = [
'<div class="sgai-cta__brand">',
' <img src="/logos/logo-color.svg" alt="" class="sgai-cta__brand-logo" />',
' <span>ScrapeGraphAI</span>',
'</div>',
'<div class="sgai-cta__title">Ready to build?</div>',
'<p class="sgai-cta__desc">Start extracting structured web data for free and scale seamlessly as your project grows.</p>',
'<a class="sgai-cta__btn sgai-cta__btn--primary" href="https://scrapegraphai.com/login">Start for free</a>',
'<a class="sgai-cta__btn sgai-cta__btn--secondary" href="https://scrapegraphai.com/pricing">See our plans</a>'
].join('');
return el;
}
function findTocList() {
var asides = document.querySelectorAll('aside, nav');
for (var i = 0; i < asides.length; i++) {
var node = asides[i];
var label = (node.getAttribute('aria-label') || '').toLowerCase();
if (label.indexOf('table of contents') !== -1 || label.indexOf('on this page') !== -1) {
var ul = node.querySelector('ul');
if (ul) return ul;
return node;
}
}
var toc = document.querySelector('#table-of-contents, [data-toc], .toc');
if (toc) return toc.querySelector('ul') || toc;
return null;
}
function inject() {
if (document.getElementById(CTA_ID)) return;
var anchor = findTocList();
if (!anchor) return;
var parent = anchor.parentElement;
if (!parent) return;
var cta = buildCta();
parent.insertBefore(cta, anchor);
}
function schedule() {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', inject);
} else {
inject();
}
}
schedule();
var observer = new MutationObserver(function () {
if (!document.getElementById(CTA_ID)) inject();
});
observer.observe(document.documentElement, { childList: true, subtree: true });
var origPush = history.pushState;
var origReplace = history.replaceState;
function onNav() { setTimeout(inject, 50); }
history.pushState = function () { var r = origPush.apply(this, arguments); onNav(); return r; };
history.replaceState = function () { var r = origReplace.apply(this, arguments); onNav(); return r; };
window.addEventListener('popstate', onNav);
// GitHub stars badge auto-update
var GH_REPO = 'ScrapeGraphAI/Scrapegraph-ai';
var GH_CACHE_KEY = 'sgai-gh-stars';
var GH_CACHE_TTL = 6 * 60 * 60 * 1000; // 6 hours
var lastStars = null;
function formatStars(n) {
if (n >= 1000) return (n / 1000).toFixed(1).replace(/\.0$/, '') + 'k+';
return String(n);
}
function updateBadgeLabel(count) {
var label = '⭐ ' + formatStars(count);
var links = document.querySelectorAll('a[href*="' + GH_REPO + '"]');
for (var i = 0; i < links.length; i++) {
var a = links[i];
var text = (a.textContent || '').trim();
if (text.indexOf('⭐') === -1) continue; // only the star badge link, not other GH links
if (text !== label) a.textContent = label;
}
}
function applyCached() {
if (lastStars != null) { updateBadgeLabel(lastStars); return true; }
try {
var cached = JSON.parse(localStorage.getItem(GH_CACHE_KEY) || 'null');
if (cached && typeof cached.n === 'number') {
lastStars = cached.n;
updateBadgeLabel(cached.n);
return true;
}
} catch (e) {}
return false;
}
function fetchStars() {
try {
var cached = JSON.parse(localStorage.getItem(GH_CACHE_KEY) || 'null');
if (cached && Date.now() - cached.t < GH_CACHE_TTL) {
lastStars = cached.n;
updateBadgeLabel(cached.n);
return;
}
} catch (e) {}
fetch('https://api.github.com/repos/' + GH_REPO)
.then(function (r) { return r.ok ? r.json() : null; })
.then(function (data) {
if (!data || typeof data.stargazers_count !== 'number') return;
var n = data.stargazers_count;
lastStars = n;
try { localStorage.setItem(GH_CACHE_KEY, JSON.stringify({ n: n, t: Date.now() })); } catch (e) {}
updateBadgeLabel(n);
})
.catch(function () {});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', fetchStars);
} else {
fetchStars();
}
var starObserver = new MutationObserver(applyCached);
starObserver.observe(document.documentElement, { childList: true, subtree: true });
function onStarsNav() { setTimeout(applyCached, 50); }
window.addEventListener('popstate', onStarsNav);
// Periodic refresh for long-lived tabs
setInterval(fetchStars, 30 * 60 * 1000);
})();