Skip to content

Commit

Permalink
temporary fix for youtube requests having to tabid
Browse files Browse the repository at this point in the history
  • Loading branch information
pouriap committed Jun 16, 2023
1 parent 30af7fc commit f4babe2
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 50 deletions.
4 changes: 2 additions & 2 deletions src/manifest-chrome.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"manifest_version": 2,
"name": "Grabby",
"description": "An extension for downloading files and media from websites.",
"version": "0.62.14",
"version_name": "0.62.14",
"version": "0.62.15",
"version_name": "0.62.15",
"author": "Pouria Pirhadi",
"homepage_url": "https://github.com/pouriap/Grabby",
"key": "AAAAB3NzaC1yc2EAAAADAQABAAABAQC0Z61bUCTt90ySxy/HiQxseplfx9D4ra1+u2x7YlnHv3kx9Q/QkD+0Q95p4m8Co7D2Zjs3cB+r6ZukhCGEUqFoU+X6dEQfAKXZPkotmtZ8sOVs+tM5S3zay0MHMSmkksEvr4Z1URaseBSg/G8+qu+CvLb4pAS0qCxhZFb8awaBERMFylD7tXta60+2/zPAvw2Wj4WmSNhtXQWzMttM3qUgsEAGJeO5p5QjiJjOjoRC/cAbkTub48A6HgduBFUqkMY2FYQFgqttySrQrJjvK+RBhEu3P6xICUTE5i4PC4q4Yl1r+cYs4mygnWZbwu3wyFl4u7OkFXUIg4QFtlKICfWp",
Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Grabby",
"description": "An extension for downloading files and media from websites.",
"version": "0.62.14",
"version": "0.62.15",
"author": "Pouria Pirhadi",
"homepage_url": "https://github.com/pouriap/Grabby",

Expand Down
104 changes: 69 additions & 35 deletions src/scripts/classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,16 +125,30 @@ class Grabby

log.warn('download is duplicate of', duplicate);

if(!download.tabId)
{
download.tabId = download.ownerTabId;
}

if(!duplicate.tabId)
{
duplicate.tabId = duplicate.ownerTabId;
}

//if the duplicate is from the same tab as the previous download do not add it
if(duplicate.tabId === download.tabId)
{
log.d('ignoring because same tab id');
return;
}

//todo: handle this
if(!download.tabId)
{
log.err('duplicate download does not have a tab id', download);
//if we are here we do not have a tab ID nor a ownertabid so it will be shown
//on every tab so no need to add it again
log.warn('duplicate download does not have a tab id', download);
return;
}

//if there is a duplicate but it's from another tab then add a salt to it
Expand Down Expand Up @@ -297,8 +311,6 @@ class BaseDownload
private _host: str_und = undefined;
private _filesize: num_und = -1;
private _fileExtension: str_und = undefined;
private _ownerTabId: num_und = undefined;
private _ownerTabUrl: str_und = undefined;
private _isFromBlankTab: bool_und = undefined;
private _tabs: SureMap<number, tabinfo>;

Expand All @@ -320,50 +332,65 @@ class BaseDownload
this._hash_src = details.url;
}

get ownerTabId(): number
get ownerTabId(): number | undefined
{
if(typeof this._ownerTabId === 'undefined')
//todo: what do?
if(typeof this.tabId === 'undefined')
{
//todo: handle this case
//if this is a download not associated with any tabs like service workers (reddit.com)
if(typeof this.tabId === 'undefined'){
log.err('download does not have a tab id', this);
for(let [tabId, tabInfo] of this._tabs.entries())
{
if(this.url === tabInfo.url)
{
return tabId;
}
log.d(this.url, ' [does not equal] ', tabInfo.url);
}

if(this.isFromBlankTab)
for(let [tabId, tabInfo] of this._tabs.entries())
{
let tab = this._tabs.getsure(this.tabId);

if(typeof tab.openerId === 'undefined')
if(this.httpDetails.documentUrl === tabInfo.url)
{
//this happens when we open a blank tab directly
//for example a bookmark to here: https://www.st.com/resource/en/datasheet/lm317.pdf
log.warn('blank tab does not have an opener id', tab);
this._ownerTabId = this.tabId;
return tabId;
}
else
}

for(let [tabId, tabInfo] of this._tabs.entries())
{
if(this.httpDetails.originUrl === tabInfo.url)
{
this._ownerTabId = tab.openerId;
return tabId;
}
}

log.warn('could not find owner tab for download', this);
return undefined;
}
else if(this.isFromBlankTab)
{
let tab = this._tabs.getsure(this.tabId);

if(typeof tab.openerId === 'undefined')
{
//this happens when we open a blank tab directly
//for example a bookmark to here: https://www.st.com/resource/en/datasheet/lm317.pdf
log.warn('blank tab does not have an opener id', tab);
return this.tabId;
}
else
{
this._ownerTabId = this.tabId;
return tab.openerId;
}
}

return this._ownerTabId;
}

get ownerTabUrl(): string
{
if(typeof this._ownerTabUrl === 'undefined')
else
{
let tab = this._tabs.getsure(this.ownerTabId);
this._ownerTabUrl = tab.url;
return this.tabId;
}
}

return this._ownerTabUrl;
get ownerTabUrl(): string | undefined
{
if(typeof this.ownerTabId == 'undefined') return undefined;
return this._tabs.getsure(this.ownerTabId).url;
}

get isFromBlankTab(): boolean
Expand Down Expand Up @@ -830,13 +857,20 @@ class StreamDownload extends GrabbedDownload implements YTDLableDownload<ytdlinf

let title = info.title;

if(typeof title === 'undefined' || !title)
if(!title)
{
title = (this.tabTitle)? this.tabTitle : 'Unknown Title';
if(title != 'Unknown Title')
if(this.tabTitle)
{
title = this.tabTitle;
if(this.ownerTabUrl)
{
let domain = Utils.getDomain(this.ownerTabUrl);
title = Utils.removeSitenameFromTitle(title, domain);
}
}
else
{
let domain = Utils.getDomain(this.ownerTabUrl);
title = Utils.removeSitenameFromTitle(title, domain);
title = 'Unknown Title';
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/scripts/download_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ class DownloadHandler implements RequestHandler
}

GB.addToAllDownloads(newDL);
Utils.showPageAction(newDL.ownerTabId);
if(newDL.ownerTabId) Utils.showPageAction(newDL.ownerTabId);

if(act === this.ACT_FORCE_DL)
{
Expand Down
5 changes: 2 additions & 3 deletions src/scripts/native_messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ namespace NativeMessaging
dl.updateData(msg.info);
dl.hidden = false;

Utils.showPageAction(dl.ownerTabId);
if(dl.ownerTabId) Utils.showPageAction(dl.ownerTabId);
}
else
{
Expand All @@ -418,8 +418,7 @@ namespace NativeMessaging
dl.updateData(info);
dl.hidden = false;

Utils.showPageAction(dl.ownerTabId);

if(dl.ownerTabId) Utils.showPageAction(dl.ownerTabId);
}
catch(e)
{
Expand Down
11 changes: 5 additions & 6 deletions src/scripts/stream_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ class StreamHandler implements RequestHandler
{
if(this.isRootManifest(manifText))
{
let tabid = this.download.tabId;
if(typeof tabid === 'undefined')
{
log.err('this main manifest does not have a tab id');
}

// make a new download object with the tab URL as the url
// currently the download object represents the manifest file but we want a
// download object that represents the page itself so the URL would be correct
// for using with YTDL

if(!this.download.tabId || !this.download.ownerTabUrl)
{
log.err('cannot find owner tab of main manifest', this.download);
}

let videoUrl = this.download.ownerTabUrl;

let details = this.download.httpDetails;
Expand Down
32 changes: 30 additions & 2 deletions src/views/popup/popup_downloads_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,36 @@ class ViewDownloadsList extends PopupView
if(download.hidden){
continue;
}

if(this.GBPop.options.showOnlyTabDls && download.ownerTabId != currTab.id){

//todo: fix
if(download.url.indexOf('youtube.com') != -1)
{
if(Utils.getDomain(currTab.url).indexOf('youtube.com') == -1)
{
continue;
}
}

else if(typeof download.ownerTabId === 'undefined')
{
if(download.httpDetails.documentUrl)
{
if(Utils.getDomain(download.httpDetails.documentUrl) != Utils.getDomain(currTab.url)){
continue;
}
}
else if(download.httpDetails.originUrl)
{
if(Utils.getDomain(download.httpDetails.originUrl) != Utils.getDomain(currTab.url)){
continue;
}
}
else if(Utils.getDomain(download.url) != Utils.getDomain(currTab.url)){
continue;
}
}

else if(this.GBPop.options.showOnlyTabDls && download.ownerTabId != currTab.id){
continue;
}

Expand Down

0 comments on commit f4babe2

Please sign in to comment.