Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a part to download current page #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 47 additions & 13 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const MAX_ITEMS = 10;

const downloads = new Map();
const pages = new Map();
const currentRequests = new Map();

const defaultOptions = {
Expand Down Expand Up @@ -60,9 +61,28 @@ function getDownloadList() {
return list;
}

function getPageRequest(pageUrl) {
var pageRequest;
for (let [reqId, req] of pages)
if (pageUrl == req.url) {
pageRequest = {
id: reqId,
url: req.url,
filename: req.filename,
size: req.size
};
}
return pageRequest;
}

function generateCommand(reqId, options) {
const request = downloads.get(reqId);
if (!request) throw new Error("Request not found");
let request = downloads.get(reqId);
if (!request) {
request = pages.get(reqId);
if (!request) {
throw new Error("Request not found");
}
}

let excludeHeaders = options.excludeHeaders
.split(" ")
Expand Down Expand Up @@ -94,6 +114,8 @@ function handleMessage(msg) {
else if (name === "getDownloadList")
return new Promise(resolve => resolve(getDownloadList()));
else if (name === "clear") return clear(...args);
else if (name === "getPageRequest")
return new Promise(resolve => resolve(getPageRequest(...args)));
else if (name === "generateCommand")
return new Promise(resolve => {
try {
Expand Down Expand Up @@ -130,7 +152,7 @@ function onBeforeRequest(details) {
}

function onSendHeaders(details) {
const req = currentRequests.get(details.requestId);
const req = currentRequests.get(details.requestId+" "+details.url);
if (req) {
req.headers = details.requestHeaders;
} else if (
Expand Down Expand Up @@ -182,26 +204,38 @@ function onResponseStarted(details) {

if (!contentDisposition || !contentDisposition.startsWith("attachment"))
if (
contentType.startsWith("text/html") ||
contentType.startsWith("text/plain") ||
contentType.startsWith("image/") ||
contentType.startsWith("application/xml")
)
) {
return;
}
if (contentType.startsWith("text/html")) {
// This is a request for a page, store it
storeRequest(pages, details, request);
return;
}

if (!request.filename)
request.filename = window.getFilenameFromUrl(request.url);

downloads.set(details.requestId, request);
storeRequest(pagres, details, request);

browser.browserAction.getBadgeText({}).then(txt => {
browser.browserAction.setBadgeText({ text: `${+txt + 1}` });
});

if (downloads.size > MAX_ITEMS) {
let keys = Array.from(downloads.keys());
keys.slice(0, keys.length - MAX_ITEMS).forEach(k => downloads.delete(k));
}
}

function storeRequest(containerMap, details, request) {
if (!request.filename)
request.filename = window.getFilenameFromUrl(request.url);

containerMap.set(details.requestId, request);

if (containerMap.size > MAX_ITEMS) {
let keys = Array.from(containerMap.keys());
keys.slice(0, keys.length - MAX_ITEMS).forEach(k => containerMap.delete(k));
}
containerMap.set(details.requestId, request);
return;
}

function onBeforeRedirect() {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"homepage_url": "https://github.com/zaidka/cliget",

"permissions": ["webRequest", "storage", "<all_urls>"],
"permissions": ["webRequest", "storage", "<all_urls>", "tabs"],

"background": {
"scripts": ["utils.js", "curl.js", "wget.js", "aria2.js", "background.js"]
Expand Down
5 changes: 5 additions & 0 deletions popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ label.text-input input[type="text"] {
.file-size {
opacity: 0.5;
}

.current-page {
border-bottom: 2px solid black;
margin: 20px;
}
16 changes: 14 additions & 2 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,18 @@ function showCommand(requestId, options) {
});
}

function showList(downloadList, highlight) {
function showList(downloadList, highlight, pageRequest) {
const body = document.body;
while (body.firstChild) body.removeChild(body.firstChild);

let currentPage = document.createElement("div");
currentPage.classList.add("panel-section", "panel-section-tabs", "current-page");
currentPage.textContent = pageRequest.url;
currentPage.onclick = function() {
showCommand(pageRequest.id);
};
body.appendChild(currentPage);

if (!downloadList.length) {
let el = document.createElement("div");
el.style.margin = "20px";
Expand Down Expand Up @@ -265,7 +273,11 @@ document.addEventListener("DOMContentLoaded", () => {
browser.browserAction.getBadgeText({}).then(txt => {
let highlight = +txt;
browser.browserAction.setBadgeText({ text: "" });
showList(list, highlight);
browser.tabs.query({'active': true, 'lastFocusedWindow': true}, tabs => {
browser.runtime.sendMessage(["getPageRequest", tabs[0].url]).then(pageRequest => {
showList(list, highlight, pageRequest);
});
});
});
});
});