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

Fixed download button #56

Open
wants to merge 5 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({result: getConfig()[request.key]});
});


chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
if(tab.url.includes('https://raw.githubusercontent.com/')){
console.log('Found raw content tab');
savePage(tab,tab.id);
}
}
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

function savePage(tab,id) {
let fileName = tab.url.split('/').slice(-1).pop(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

console.log(`Found Path ${tab.url}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'template literal syntax' is only available in ES6 (use 'esversion: 6').

console.log(`Found file name ${fileName}`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'template literal syntax' is only available in ES6 (use 'esversion: 6').

console.log(`Closing tab & sending download`);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'template literal syntax' is only available in ES6 (use 'esversion: 6').

chrome.tabs.remove(id);
if(fileName.charAt(0) === '.') fileName = fileName.slice(1) + '.txt';
chrome.downloads.download({
url: tab.url,
filename: fileName
});
}



var GitHubNotification;
var notificationUrl = 'https://github.com/notifications';
var blue = [1, 128, 255, 255];
Expand Down Expand Up @@ -36,7 +60,6 @@ function _goToNotificationTab() {
// type can be unread/participating
function _extractUnreadNotifications(response) {
var type = getConfig()['feature-2-type'];

if (type === 'participating') {
return parseInt(response.match(/<span class="count">(\d+)</g)[1].match(/\d+/g)[0]);
}
Expand Down
6 changes: 5 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
},
"permissions" : [
"*://github.com/*",
"*://raw.githubusercontent.com/*",
"tabs",
"webNavigation"
"webNavigation",
"activeTab",
"pageCapture",
"downloads"
],
"web_accessible_resources": ["page.js"],
"content_scripts": [
Expand Down
35 changes: 22 additions & 13 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ var s = document.createElement('script');
(document.head||document.documentElement).appendChild(s);
// page.js injection end


var FileDownloader = {
init: function() {
FileDownloader.addDownloadTooltip();
FileDownloader.bindPopState();

chrome.runtime.sendMessage({key: "feature-1-enable"}, function(response) {
if(typeof(response.result) === 'undefined' || response.result === true) {
$(document).on('click', '.octicon-file-text', function(e) {
// Changed .octicon-file-text to .download-btn
$(document).on('click', '.download-btn', function(e) {
FileDownloader.eventHandler(e);
});
} else {
Expand All @@ -25,7 +24,7 @@ var FileDownloader = {
},

addDownloadTooltip: function() {
Array.prototype.slice.call(document.querySelectorAll('.octicon-file-text')).map(function(icon) {
Array.prototype.slice.call(document.querySelectorAll('.octicon-file')).map(function(icon) {
var td = icon.parentNode;
td.classList.add('tooltipped', 'tooltipped-se');
td.setAttribute('aria-label', 'Click to download');
Expand All @@ -36,8 +35,10 @@ var FileDownloader = {
var fileName = document.querySelector('.breadcrumb .final-path').textContent;
let btn = document.createElement('a');
btn.setAttribute('class', 'btn btn-sm btn-primary tooltipped tooltipped-n download-btn');
btn.setAttribute('href', rawUrlNode.href);
btn.setAttribute('download', fileName);
btn.setAttribute('target','_blank');
btn.setAttribute('rel','noreferrer');
// btn.setAttribute('href', rawUrlNode.href);
btn.setAttribute('download',fileName);
btn.setAttribute('aria-label', 'Click to download ' + fileName);
btn.textContent = 'Download';
rawUrlNode.parentNode.parentNode.prepend(btn);
Expand All @@ -57,26 +58,34 @@ var FileDownloader = {

eventHandler: function(event) {
if (this.isFromListPage(event.currentTarget)) {
var linkNode = event.currentTarget.parentNode.nextElementSibling.querySelector('a');
var href = linkNode.href.replace('\/blob\/', '\/raw\/');
this.downloadIt(href, linkNode.textContent);
var linkNode = event.currentTarget.parentNode.nextElementSibling.querySelectorAll('a:not(.tooltipped)')[0];
// var href = linkNode.href.replace('\/blob\/', '\/raw\/');
let href = linkNode.href.replace('/raw/','/');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

href = href.replace('https://github.com/','https://raw.githubusercontent.com/');
// Change linkNode.textContent to event.target.download
this.downloadIt(href, event.target.download);
}
},

downloadIt: function(href, fileName) {
var downloadNode = document.createElement('a');
downloadNode.setAttribute('href', href);
downloadNode.setAttribute('rel','noopener noreferrer');
downloadNode.setAttribute('target','_blank');
downloadNode.setAttribute('download', fileName);
downloadNode.click();
downloadNode = null;
},

isFromListPage: function(node) {
return node.classList.contains('octicon-file-text') &&
document.querySelector('.files') &&
document.querySelector('.files').contains(node) &&
document.querySelector('.file') === null;
return node.classList.contains('download-btn') //&& // octicon-file // octicon-file-text
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing semicolon.

// document.querySelector('.files') &&
// document.querySelector('.files').contains(node) &&
// document.querySelector('.file') === null;
},
}

FileDownloader.init();