Extracts and displays interesting facts about cockatoos from Wikipedia
Size
6.6 KB
Version
1.0.1
Created
Mar 27, 2026
Updated
20 days ago
1// ==UserScript==
2// @name Cockatoo Fact Extractor
3// @description Extracts and displays interesting facts about cockatoos from Wikipedia
4// @version 1.0.1
5// @match https://*.en.wikipedia.org/*
6// @icon https://en.wikipedia.org/static/favicon/wikipedia.ico
7// ==/UserScript==
8(function() {
9 'use strict';
10
11 // Function to extract cockatoo facts from the Wikipedia page
12 function extractCockatooFacts() {
13 const facts = [];
14
15 // Get the main content area
16 const content = document.getElementById('mw-content-text');
17 if (!content) return facts;
18
19 // Look for headings and their associated paragraphs
20 const headings = content.querySelectorAll('h2, h3, h4');
21
22 headings.forEach(heading => {
23 const headingText = heading.textContent.trim();
24
25 // Check if the heading is relevant to cockatoo facts
26 if (isRelevantHeading(headingText)) {
27 const paragraph = getParagraphAfterHeading(heading);
28 if (paragraph) {
29 facts.push({
30 category: headingText,
31 fact: paragraph.textContent.trim()
32 });
33 }
34 }
35 });
36
37 return facts;
38 }
39
40 // Function to determine if a heading is relevant to cockatoo facts
41 function isRelevantHeading(headingText) {
42 const relevantKeywords = [
43 'behavior', 'behaviour', 'diet', 'habitat', 'breeding',
44 'conservation', 'characteristics', 'description', 'taxonomy',
45 'distribution', 'ecology', 'intelligence', 'social'
46 ];
47
48 const lowerHeading = headingText.toLowerCase();
49 return relevantKeywords.some(keyword => lowerHeading.includes(keyword));
50 }
51
52 // Function to get the paragraph immediately following a heading
53 function getParagraphAfterHeading(heading) {
54 let sibling = heading.nextElementSibling;
55
56 // Skip any elements that aren't paragraphs
57 while (sibling && sibling.tagName !== 'P') {
58 sibling = sibling.nextElementSibling;
59 }
60
61 return sibling;
62 }
63
64 // Function to create and display the facts panel
65 function displayFacts(facts) {
66 // Remove existing panel if it exists
67 const existingPanel = document.getElementById('cockatoo-facts-panel');
68 if (existingPanel) {
69 existingPanel.remove();
70 }
71
72 if (facts.length === 0) {
73 console.log('No cockatoo facts found on this page');
74 return;
75 }
76
77 // Create the panel container
78 const panel = document.createElement('div');
79 panel.id = 'cockatoo-facts-panel';
80 panel.style.cssText = `
81 position: fixed;
82 top: 20px;
83 right: 20px;
84 width: 350px;
85 max-height: 80vh;
86 background: #f9f9f9;
87 border: 2px solid #ccc;
88 border-radius: 8px;
89 padding: 15px;
90 box-shadow: 0 4px 8px rgba(0,0,0,0.2);
91 z-index: 10000;
92 overflow-y: auto;
93 font-family: sans-serif;
94 `;
95
96 // Add header
97 const header = document.createElement('h2');
98 header.textContent = 'Cockatoo Facts';
99 header.style.cssText = `
100 margin-top: 0;
101 color: #333;
102 border-bottom: 2px solid #ddd;
103 padding-bottom: 10px;
104 `;
105 panel.appendChild(header);
106
107 // Add close button
108 const closeButton = document.createElement('button');
109 closeButton.textContent = '×';
110 closeButton.style.cssText = `
111 position: absolute;
112 top: 10px;
113 right: 10px;
114 background: none;
115 border: none;
116 font-size: 24px;
117 cursor: pointer;
118 color: #999;
119 `;
120 closeButton.addEventListener('click', () => panel.remove());
121 panel.appendChild(closeButton);
122
123 // Add facts
124 facts.forEach(factObj => {
125 const factContainer = document.createElement('div');
126 factContainer.style.cssText = `
127 margin-bottom: 15px;
128 padding: 10px;
129 background: white;
130 border-radius: 5px;
131 border-left: 4px solid #4CAF50;
132 `;
133
134 const category = document.createElement('h4');
135 category.textContent = factObj.category;
136 category.style.cssText = `
137 margin: 0 0 8px 0;
138 color: #4CAF50;
139 `;
140
141 const fact = document.createElement('p');
142 fact.textContent = factObj.fact;
143 fact.style.cssText = `
144 margin: 0;
145 font-size: 14px;
146 line-height: 1.4;
147 color: #555;
148 `;
149
150 factContainer.appendChild(category);
151 factContainer.appendChild(fact);
152 panel.appendChild(factContainer);
153 });
154
155 // Add to document
156 document.body.appendChild(panel);
157 }
158
159 // Main initialization function
160 function init() {
161 // Only run on Wikipedia pages
162 if (!window.location.hostname.includes('wikipedia.org')) {
163 return;
164 }
165
166 // Wait for the page to load
167 if (document.readyState === 'loading') {
168 document.addEventListener('DOMContentLoaded', runExtractor);
169 } else {
170 runExtractor();
171 }
172 }
173
174 // Run the extractor
175 function runExtractor() {
176 // Add a button to trigger fact extraction
177 const toolbar = document.getElementById('mw-toolbar');
178 if (toolbar) {
179 const button = document.createElement('button');
180 button.textContent = 'Extract Cockatoo Facts';
181 button.style.cssText = `
182 margin-left: 10px;
183 padding: 5px 10px;
184 background: #4CAF50;
185 color: white;
186 border: none;
187 border-radius: 4px;
188 cursor: pointer;
189 `;
190 button.addEventListener('click', () => {
191 const facts = extractCockatooFacts();
192 displayFacts(facts);
193 });
194 toolbar.appendChild(button);
195 } else {
196 // If no toolbar found, run automatically after a delay
197 setTimeout(() => {
198 const facts = extractCockatooFacts();
199 displayFacts(facts);
200 }, 2000);
201 }
202 }
203
204 // Start the extension
205 init();
206})();