Skip to content

Commit

Permalink
Updated to work with new page layout, added locales
Browse files Browse the repository at this point in the history
  • Loading branch information
seszele64 committed Mar 23, 2024
1 parent d1d28af commit c823b80
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 107 deletions.
20 changes: 20 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"popupTitle": {
"message": "Find documents on Docer"
},
"instructionsTitle": {
"message": "Instructions"
},
"instructionsText": {
"message": "Simply open the Docer site and navigate to the document you wish to download. Once there, perform captcha verification to start the download."
},
"authorText": {
"message": "Author: seszele"
},
"projectLinkText": {
"message": "Project link, leave ⭐"
},
"latestReleaseText": {
"message": "Latest release"
}
}
21 changes: 21 additions & 0 deletions _locales/pl/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"popupTitle": {
"message": "Znajdź i pobierz dokumenty z Docer.pl"
},
"instructionsTitle": {
"message": "Instrukcje"
},
"instructionsText": {
"message": "Po prostu otwórz stronę Docer i przejdź do dokumentu, który chcesz pobrać. Na niej, wykonaj weryfikację captcha, aby rozpocząć pobieranie."
},
"authorText": {
"message": "Autor: seszele"
},
"projectLinkText": {
"message": "zostaw ⭐ na GitHubie"
},
"latestReleaseText": {
"message": "Najnowsza wersja"
}
}

52 changes: 9 additions & 43 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,10 @@
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === "download" && message.itemId) {
// Random number between 1 and 9
const rc = Math.floor(Math.random() * 9) + 1;
const bodyData = new URLSearchParams({
item_id: message.itemId,
rc: rc
}).toString();

fetch("https://docer.pl/start/download", {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Pragma": "no-cache",
"X-Requested-With": "XMLHttpRequest",
"Referer": message.referer,
"Origin": "https://docer.pl",
},
body: bodyData,
credentials: "include"
})
.then(response => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
})
.then(data => {
if (data && data.success && data.response && data.response.url) {
chrome.tabs.create({ url: data.response.url }); // Open in a new tab
} else {
throw new Error('Request succeeded but no URL was provided in the response.');
}
})
.catch(error => {
console.error('Error during fetch:', error);
sendResponse({ error: error.message });
// background.js

chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
if (message.action === "openTab" && message.url) {
// Open the PDF URL in a new tab
chrome.tabs.create({ url: message.url }, function(tab) {
console.log('New tab opened with PDF:', message.url);
});
}
return true; // Required to use sendResponse asynchronously
});

}
});
59 changes: 43 additions & 16 deletions contentScript.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
// This could be triggered by some event on the page, e.g., button click
document.addEventListener('click', function(event) {
if (event.target.matches('#dwn_btn')) {
const item_id = event.target.getAttribute('data-id');
const referer = window.location.href;

// Send the item_id to the background script
chrome.runtime.sendMessage({action: "download", itemId: item_id, referer: referer}, function(response) {
if(response && response.error) {
console.error('Error in background script:', response.error);
} else {
// Handle successful response or further actions here
}
});
// contentScript.js

}
});
// Function to set up the observer
function setUpObserver() {
const pdfHolder = document.getElementById('pdf-holder');
if (pdfHolder) {
// Create an observer instance linked to the checkForPDFUrl callback function
const observer = new MutationObserver(checkForPDFUrl);

// Start observing the target node for configured mutations
observer.observe(pdfHolder, { childList: true, subtree: true });

console.log('Observer has been set up.');
} else {
console.log('The PDF holder element does not exist on this page.');
}
}

function checkForPDFUrl(mutationList, observer) {
for (const mutation of mutationList) {
if (mutation.type === 'childList') {
const pdfHolder = document.getElementById('pdf-holder');
const pdfUrlElement = pdfHolder.querySelector('[data-pdf-url]');
if (pdfUrlElement) {
const pdfUrl = pdfUrlElement.getAttribute('data-pdf-url');
if (pdfUrl) {
// Send the PDF URL to the background script
chrome.runtime.sendMessage({action: "openTab", url: pdfUrl});
observer.disconnect(); // Disconnect the observer if not needed further
console.log('PDF URL found and sent to background script:', pdfUrl);
}
}
}
}
}

// Wait for the DOM to be fully loaded before attempting to set up the observer
if (document.readyState === "complete" || document.readyState === "interactive") {
// DOM is already ready to be manipulated
setUpObserver();
} else {
// Set up observer once DOM is fully loaded
window.addEventListener('DOMContentLoaded', setUpObserver);
}
10 changes: 10 additions & 0 deletions localize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
document.addEventListener('DOMContentLoaded', function() {
var elements = document.querySelectorAll('[data-i18n]');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
var message = chrome.i18n.getMessage(element.getAttribute('data-i18n'));
if (message) {
element.innerHTML = message;
}
}
});
15 changes: 10 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"manifest_version": 3,
"name": "Docer Downloader",
"version": "1.1",
"version": "1.2",
"description": "A Chrome extension designed to facilitate the download of documents exceeding 1 MB on docer.pl without encountering payment requests.",
"default_locale": "en",
"permissions": [
"activeTab",
"tabs"
"tabs",
"activeTab"
],
"host_permissions": [
"https://docer.pl/*"
],
"background": {
"service_worker": "background.js"
Expand All @@ -21,7 +25,8 @@
"content_scripts": [
{
"matches": ["https://docer.pl/*"],
"js": ["contentScript.js"]
"js": ["contentScript.js"],
"run_at": "document_start"
}
]
}
}
60 changes: 41 additions & 19 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,67 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Check for PDF</title>
<title>Docer</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css">
<style>
body {
width: 300px;
min-height: 200px; /* Adjusted for additional content */
padding: 15px;
width: 350px; /* Slightly wider for a better layout */
min-height: 200px;
padding: 20px;
font-family: 'Arial', sans-serif;
background-color: #f8f9fa; /* Soft background color */
}
.card {
margin-bottom: 10px;
margin-bottom: 20px; /* More space below card */
border: none; /* Remove card border */
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* Add subtle shadow to card */
}
.btn {
width: 100%;
margin-top: 10px;
.card-title {
color: #495057; /* Dark gray color for text */
font-size: 1.25rem; /* Larger font size for title */
margin-bottom: 15px; /* More space below title */
}
.card-text {
color: #6c757d; /* Soft color for text */
line-height: 1.5; /* Improved line spacing for readability */
}
.footer {
text-align: center;
font-size: 0.8em;
margin-top: 15px;
font-size: 0.9em; /* A bit larger font size */
color: #6c757d; /* Soft color for footer text */
margin-top: 20px; /* More space above footer */
}
.footer a {
color: #007bff; /* Bootstrap primary color for links */
text-decoration: none; /* No underline on links */
margin-right: 5px; /* Spacing between links */
}
.footer a:hover {
color: #0056b3; /* Darken link color on hover */
}
.footer i {
color: #ffc107; /* Yellow color for the star */
}
/* Additional custom styles can go here */
</style>
</head>
<body>
<div class="card">
<div class="card-body">
<h5 class="card-title">Instructions</h5>
<p class="card-text">
Simply open the Docer site and navigate to the document you wish to download. Once there, click the button below to initiate the download process.
</p>
<button id="download" class="btn btn-primary">Download Document</button>
<h5 class="card-title" data-i18n="instructionsTitle"></h5>
<p class="card-text" data-i18n="instructionsText"></p>
</div>
</div>
<div class="footer">
<p>Author: seszele</p>
<p data-i18n="authorText"></p>

<p>
<a href="https://github.com/seszele64/docer-downloader" target="_blank">Link to the project</a> |
<a href="https://github.com/seszele64/docer-downloader/releases/latest" target="_blank">Latest release</a>
<a href="https://github.com/seszele64/docer-downloader" target="_blank" data-i18n="projectLinkText"></a> |
<a href="https://github.com/seszele64/docer-downloader/releases/latest" target="_blank" data-i18n="latestReleaseText"></a>
</p>

</div>
<script src="popup.js"></script>
<script src="localize.js"></script>
</body>
</html>
24 changes: 0 additions & 24 deletions popup.js

This file was deleted.

0 comments on commit c823b80

Please sign in to comment.