Skip to content

Commit

Permalink
feat: add badges for project and repository view counts with copy fun…
Browse files Browse the repository at this point in the history
…ctionality (#3215)
  • Loading branch information
JisanAR03 authored Jan 11, 2025
1 parent f7151c0 commit cf4cc3b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p align="center"><a href="https://github.com/OWASP/BLT/actions" rel="noopener noreferrer" target="__blank"><img alt="Build" src="https://github.com/OWASP/BLT/actions/workflows/auto-merge.yml/badge.svg"></a> <a href="https://github.com/OWASP/BLT/blob/main/LICENSE.md" rel="noopener noreferrer"><img src="https://img.shields.io/badge/license-AGPL--3.0-blue"></a>
<a href="https://github.com/OWASP/BLT" rel="noopener noreferrer" target="__blank"><img alt="GitHub stars" src="https://img.shields.io/github/stars/OWASP/BLT?style=social"></a></p>

<img alt="Views" src="https://blt.owasp.org/projects/blt/badge"></a>
<img alt="Views" src="https://blt.owasp.org/repos/blt/badge/">

Everything is on our <a href="https://blt.owasp.org">homepage</a>

Expand Down
55 changes: 55 additions & 0 deletions website/templates/projects/project_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,27 @@ <h1 class="text-3xl font-bold text-white mb-2">{{ project.name }}</h1>
</div>
</div>
</div>
<!-- Badge URL Section -->
<div class="p-6 bg-gray-50 border-t border-gray-200">
<h3 class="text-lg font-semibold text-gray-900 mb-4">Project View Count Badge</h3>
<div class="flex flex-col gap-4">
<!-- HTML Format -->
<div class="space-y-2">
<div class="text-gray-700 font-medium">HTML:</div>
<div class="relative">
<input type="text"
readonly
value='&lt;img alt="Views" src="{{ request.scheme }}://{{ request.get_host }}{% url 'project-badge' project.slug %}" &gt;'
class="w-full px-4 py-2 bg-white border border-gray-300 rounded-lg pr-24 font-mono text-sm focus:ring-2 focus:ring-red-500 focus:border-transparent"
id="badge-url-html">
<button onclick="copyToClipboard('badge-url-html')"
class="absolute right-2 top-1/2 -translate-y-1/2 px-3 py-1 bg-red-500 hover:bg-red-600 text-white text-sm rounded-md transition-colors">
Copy
</button>
</div>
</div>
</div>
</div>
</div>
<!-- Associated Repositories -->
<div class="grid gap-6">
Expand Down Expand Up @@ -287,3 +308,37 @@ <h2 class="text-xl font-bold text-gray-900 mb-4">Project Timeline</h2>
</div>
</div>
{% endblock content %}
{% block after_js %}
<script>
function copyToClipboard(elementId) {
const element = document.getElementById(elementId);

// Select the text
element.select();
element.setSelectionRange(0, 99999); // For mobile devices

// Copy the text
try {
navigator.clipboard.writeText(element.value).then(() => {
// Get the button
const button = element.nextElementSibling;
const originalText = button.textContent;

// Change button style to show success
button.textContent = 'Copied!';
button.classList.remove('bg-red-500', 'hover:bg-red-600');
button.classList.add('bg-green-500', 'hover:bg-green-600');

// Reset button after 2 seconds
setTimeout(() => {
button.textContent = originalText;
button.classList.remove('bg-green-500', 'hover:bg-green-600');
button.classList.add('bg-red-500', 'hover:bg-red-600');
}, 2000);
});
} catch (err) {
console.error('Failed to copy text: ', err);
}
}
</script>
{% endblock %}
54 changes: 53 additions & 1 deletion website/templates/projects/repo_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ <h1 class="text-3xl font-bold text-gray-900">{{ repo.name }}</h1>
</a>
</div>
</div>
<!-- Badge URL Section -->
<div class="mt-6 p-4 bg-gray-50 rounded-lg border border-gray-200">
<h3 class="text-lg font-semibold text-gray-900 mb-4">Repository View Count Badge</h3>
<div class="flex flex-col gap-4">
<!-- HTML Format -->
<div class="space-y-2">
<div class="text-gray-700 font-medium">HTML:</div>
<div class="relative">
<input type="text"
readonly
value='&lt;img alt="Views" src="{{ request.scheme }}://{{ request.get_host }}{% url 'repo-badge' repo.slug %}" &gt;'
class="w-full px-4 py-2 bg-white border border-gray-300 rounded-lg pr-24 font-mono text-sm focus:ring-2 focus:ring-red-500 focus:border-transparent"
id="badge-url-html">
<button onclick="copyToClipboard('badge-url-html')"
class="absolute right-2 top-1/2 -translate-y-1/2 px-3 py-1 bg-red-500 hover:bg-red-600 text-white text-sm rounded-md transition-colors">
Copy
</button>
</div>
</div>
</div>
</div>
<!-- Repository Type & Metadata -->
<div class="mt-6 flex items-center justify-between border-t border-gray-200 pt-6">
<div class="flex items-center gap-6">
Expand Down Expand Up @@ -740,7 +761,38 @@ <h3 class="font-medium text-gray-900 group-hover:text-red-600 transition-colors"
{% endblock content %}
{% block after_js %}
<script>
const refreshSection = async (button, section) => {
function copyToClipboard(elementId) {
const element = document.getElementById(elementId);

// Select the text
element.select();
element.setSelectionRange(0, 99999); // For mobile devices

// Copy the text
try {
navigator.clipboard.writeText(element.value).then(() => {
// Get the button
const button = element.nextElementSibling;
const originalText = button.textContent;

// Change button style to show success
button.textContent = 'Copied!';
button.classList.remove('bg-red-500', 'hover:bg-red-600');
button.classList.add('bg-green-500', 'hover:bg-green-600');

// Reset button after 2 seconds
setTimeout(() => {
button.textContent = originalText;
button.classList.remove('bg-green-500', 'hover:bg-green-600');
button.classList.add('bg-red-500', 'hover:bg-red-600');
}, 2000);
});
} catch (err) {
console.error('Failed to copy text: ', err);
}
}

const refreshSection = async (button, section) => {
if (button.classList.contains('refresh_spinner')) {
return;
}
Expand Down

0 comments on commit cf4cc3b

Please sign in to comment.