Advertisement
ERROR_CODE

wikipedia

Dec 3rd, 2024 (edited)
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.95 KB | None | 0 0
  1. local HttpService = game:GetService("HttpService")
  2. local Players = game:GetService("Players")
  3.  
  4. -- Создаем ScreenGui
  5. local screenGui = Instance.new("ScreenGui")
  6. screenGui.Parent = Players.LocalPlayer:WaitForChild("PlayerGui")
  7.  
  8. -- Создаем TextBox для ввода запроса
  9. local searchBox = Instance.new("TextBox")
  10. searchBox.Size = UDim2.new(0.5, 0, 0.1, 0)
  11. searchBox.Position = UDim2.new(0.25, 0, 0.1, 0)
  12. searchBox.PlaceholderText = "Введите запрос"
  13. searchBox.Parent = screenGui
  14.  
  15. -- Создаем ScrollingFrame для отображения результатов
  16. local scrollingFrame = Instance.new("ScrollingFrame")
  17. scrollingFrame.Size = UDim2.new(0.5, 0, 0.5, 0)
  18. scrollingFrame.Position = UDim2.new(0.25, 0, 0.25, 0)
  19. scrollingFrame.CanvasSize = UDim2.new(0, 0, 2, 0) -- Увеличиваем размер холста для прокрутки
  20. scrollingFrame.Parent = screenGui
  21.  
  22. -- URL API Википедии
  23. local apiUrl = "https://ru.wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch="
  24.  
  25. -- Функция для загрузки данных
  26. local function loadResults(query)
  27.     local response = game:HttpGetAsync(apiUrl .. HttpService:UrlEncode(query))
  28.     local data = HttpService:JSONDecode(response)
  29.     return data.query.search
  30. end
  31.  
  32. -- Функция для очистки текста от HTML-тегов
  33. local function cleanText(text)
  34.     -- Убираем теги <span class="searchmatch"></span>
  35.     text = text:gsub('<span class="searchmatch">', '')
  36.     text = text:gsub('</span>', '')
  37.     return text
  38. end
  39.  
  40. -- Функция для создания блока
  41. local function createBlock(result)
  42.     local block = Instance.new("TextButton")
  43.     block.Size = UDim2.new(1, 0, 0, 50)
  44.     block.Text = result.title
  45.     block.TextScaled = true
  46.     block.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
  47.     block.BorderSizePixel = 1
  48.     block.Parent = scrollingFrame
  49.  
  50.     -- Добавляем событие нажатия
  51.     block.MouseButton1Click:Connect(function()
  52.         local explanation = Instance.new("TextLabel")
  53.         explanation.Size = UDim2.new(1, 0, 0, 100)
  54.         explanation.Position = UDim2.new(0, 0, 0, block.AbsolutePosition.Y + block.AbsoluteSize.Y)
  55.         explanation.Text = cleanText("Это из API Википедии: " .. result.snippet)
  56.         explanation.TextScaled = true
  57.         explanation.BackgroundColor3 = Color3.fromRGB(240, 240, 240)
  58.         explanation.BorderSizePixel = 1
  59.         explanation.Parent = scrollingFrame
  60.     end)
  61. end
  62.  
  63. -- Функция для обработки запроса
  64. local function onSearch()
  65.     scrollingFrame:ClearAllChildren()
  66.     local query = searchBox.Text
  67.     local results = loadResults(query)
  68.     for _, result in ipairs(results) do
  69.         createBlock(result)
  70.     end
  71. end
  72.  
  73. -- Подключение функции к событию изменения текста
  74. searchBox.FocusLost:Connect(onSearch)
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement