Skip to content

xDarkheim/HTML-ELEMENT-COUNTER

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTML Element Counter

A simple web application that counts HTML elements on any webpage and provides statistics about requests.

What it does

  • Takes a URL and HTML element name (like 'img', 'div', 'p')
  • Fetches the webpage and counts how many times that element appears
  • Stores results in MySQL database or MariaDB
  • Shows statistics about all requests made
  • Caches results for 5 minutes to avoid repeated requests

Features

  • Element Counting: Uses PHP DOMDocument to parse HTML and count elements
  • 5-minute Caching: Same URL+element requests return cached results
  • Statistics: Shows domain stats, fetch times, and global element counts
  • Database Storage: Normalized MySQL database with separate tables
  • Error Handling: Graceful handling of failed requests and invalid URLs
  • AJAX Interface: No page reload needed

Technology Stack

  • Backend: PHP 8+ with PDO
  • Database: MySQL
  • Frontend: Vanilla JavaScript (no frameworks)
  • Styling: Simple CSS
  • Dependencies: Composer for autoloading

Installation

  1. Clone or download this project
  2. Install dependencies:
    composer install
  3. Create database in MySQL
  4. Create .env file in project root:
    DB_HOST=localhost
    DB_DATABASE=your_database_name
    DB_USERNAME=your_username
    DB_PASSWORD=your_password
    DB_CHARSET=utf8mb4
    
  5. Import database tables from docs/SQL/:
    • Run domain.sql
    • Run elements.sql
    • Run urls.sql
    • Run requests.sql

Usage

  1. Open index.php in your web browser
  2. Enter a website URL
  3. Enter HTML element name (e.g., img, div, p, a)
  4. Click "Count Elements"
  5. View results and statistics

Project Structure

β”œβ”€β”€ index.php              # Main page with form
β”œβ”€β”€ api/count.php          # API endpoint for AJAX requests
β”œβ”€β”€ assets/                # CSS and JavaScript files
β”œβ”€β”€ src/                   # PHP classes
β”‚   β”œβ”€β”€ Interface/         # Interfaces for services
β”‚   β”œβ”€β”€ Database/          # Database connection
β”‚   β”œβ”€β”€ Models/            # Domain, URL, Element, Request models
β”‚   β”œβ”€β”€ Services/          # Counter, Fetcher, ErrorHandler
β”‚   β”œβ”€β”€ Utils/             # Environment loader
β”‚   └── Validators/        # Input validation
β”œβ”€β”€ docs/SQL/              # Database schema files
└── vendor/                # Composer dependencies

How it works

  1. User submits form β†’ JavaScript sends AJAX request to api/count.php
  2. API validates input β†’ URL and element name checked
  3. Check cache β†’ Look for the same request made within 5 minutes
  4. If not cached:
    • Fetch webpage using cURL
    • Parse HTML with DOMDocument
    • Count specified elements
    • Save to the database
  5. Generate statistics from the database
  6. Return JSON response with count and stats

Database Schema

  • domains: Stores unique domain names
  • urls: Stores full URLs linked to domains
  • elements: Stores HTML element names (img, div, etc.)
  • requests: Stores each counting request with results

Requirements

  • PHP 8.0+
  • MySQL 5.7+
  • Web server (Apache/Nginx)
  • cURL extension enabled
  • DOM extension enabled

Tested Browsers

This application has been tested and works correctly in:

  • βœ… Google Chrome
  • βœ… Chromium
  • βœ… Mozilla Firefox
  • βœ… Opera
  • βœ… Brave

Code Quality

This project uses PHPStan for static code analysis. PHPStan checks the code without running it and finds potential bugs.

Running PHPStan

./vendor/bin/phpstan analyse

Current level: 5 (optimal balance between strictness and practicality)

See docs/PHPSTAN.md for detailed documentation about static analysis.

Limitations

  • Some websites may block automated requests
  • Large pages may take longer to fetch and parse
  • No authentication or rate limiting implemented
  • Basic error messages (could be more user-friendly)

Troubleshooting

"Database connection failed": Check .env file and MySQL credentials "Invalid JSON": Check if API endpoint is accessible "Element count 0": Website might be blocking requests or element doesn't exist "Something went wrong": Check browser console and server error logs

Time Spent on Development

  • Planning and architecture: 2 hours
  • Database design and setup: 1 hour
  • Backend PHP development: 4 hours
  • Frontend JavaScript/CSS: 2 hours
  • Testing and debugging: 3 hours
  • Documentation: 1 hour

Total: ~13 hours

About

HTML-ELEMENT-COUNTER

Resources

Stars

Watchers

Forks

Contributors