Skip to content

Commit

Permalink
Some bugfixes and new profile for animetosho
Browse files Browse the repository at this point in the history
  • Loading branch information
clutterskull committed Feb 21, 2022
1 parent 15c1f88 commit 0ac35c3
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dist/*
node_modules

.test
.env.json
.env*

# Yarn
.yarn/*
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nzbunity",
"version": "1.16.2",
"version": "1.17.0",
"license": "MPL-2.0",
"description": "Send and control NZB files directly with SABnzbd or NZBGet download clients.",
"author": "clutterskull@gmail.com",
Expand Down
31 changes: 31 additions & 0 deletions src/content/sites/animetosho.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class NZBUnityAnimeTosho {
constructor() {
Util.storage.get('Providers')
.then((opts) => {
const provider = opts.Providers && opts.Providers.animetosho;
const enabled:boolean = provider ? provider.Enabled : true;

if (enabled) {
console.info(`[NZB Unity] Initializing 1-click functionality...`);
this.initializeLinks();
} else {
console.info(`[NZB Unity] 1-click functionality disabled for this site`);
}
});
}

initializeLinks() {
$('a[href*="/nzbs/"]').each((i, el) => {
const a = $(el);
PageUtil.createAddUrlLink({
url: a.attr('href'),
})
.css({ margin: '0 0 0 3px', 'vertical-align': 'middle' })
.insertAfter(a);
});
}
}

$(($) => {
const nzbIntegration = new NZBUnityAnimeTosho();
});
7 changes: 6 additions & 1 deletion src/content/sites/newznab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,12 @@ class NZBUnityNewznab {
if (this.replace) {
a.replaceWith(link);
} else {
link.css({ margin: '0 .2em 0 .5em' });
if (/nzbplanet\.net/i.test(window.location.host)) {
a.closest('td').css({ width: '62px' });
link.css({ margin: '2px 0 0 3px' });
} else {
link.css({ margin: '0 .2em 0 .5em' });
}
link.appendTo(a.closest('td'));
}
});
Expand Down
121 changes: 18 additions & 103 deletions src/content/sites/nzbking.ts
Original file line number Diff line number Diff line change
@@ -1,125 +1,40 @@
class NZBUnityNzbking {
public form:JQuery<HTMLElement>;
public csrfToken:string;
public isList:boolean;
public isDetail:boolean;

constructor() {
Util.storage.get('Providers')
.then((opts) => {
let provider = opts.Providers && opts.Providers.nzbking;
let enabled:boolean = provider ? provider.Enabled : true;
const provider = opts.Providers && opts.Providers.nzbking;
const enabled:boolean = provider ? provider.Enabled : true;

if (enabled) {
console.info(`[NZB Unity] Initializing 1-click functionality...`);
this.form = $('form[name="r"]');
this.csrfToken = <string> this.form.find('input[name="csrfmiddlewaretoken"]').val();
this.isList = /^\/(group|search)/.test(window.location.pathname);
this.isDetail = /^\/(details)/.test(window.location.pathname);

if (this.form && this.csrfToken) {
console.info(this.csrfToken);
this.initializeLinks();
} else {
console.error(`[NZB Unity] Could not locate csrf token, 1-click disabled`);
}
this.initializeLinks();
} else {
console.info(`[NZB Unity] 1-click functionality disabled for this site`);
}
});
}

getNzbIds():string[] {
return this.form.serializeArray()
.filter((i) => { return i.name === 'nzb' })
.map((i) => { return i.value });
getCsrfToken():string {
return String($('input[name="csrfmiddlewaretoken"]').val());
}

initializeLinks() {
// Create direct download button
this.form.find('[type="submit"]').each((i, el) => {
let submit:JQuery<HTMLElement> = $(el);
let button:JQuery<HTMLElement> = PageUtil.createButton()
.on('click', (e) => {
e.preventDefault();

let nzbIds:string[] = this.getNzbIds();
if (nzbIds.length) {
console.info(`[NZB Unity] Adding ${nzbIds.length} NZB(s)`);
button.trigger('nzb.pending');

Promise.all(nzbIds.map((nzbId) => {
return PageUtil.requestAndAddFile(
nzbId,
'',
'http://nzbking.com/nzb/',
{
csrfmiddlewaretoken: this.csrfToken,
nzb: nzbId
}
)
.catch((e) => {
console.error(`[NZB Unity] Error fetching NZB content (${nzbId}): ${e.status} ${e.statusText}`);
});
}))
.then((results:any[]) => {
setTimeout(() => {
if (results.some((r) => { return r === false; })) {
button.trigger('nzb.failure');
} else {
button.trigger('nzb.success');
}
}, 1000);
});
}
})
.insertBefore(submit);
getNzbUrl(id:string):string {
return `${window.location.origin}/nzb:${id}/`;
}

if (this.isDetail) {
button.text('Download All').attr('title', 'Download with NZB Unity');
}
initializeLinks() {
$('a[href^="/nzb:"]').each((i, el) => {
const a = $(el);
PageUtil.createAddUrlLink({
url: `${window.location.origin}${a.attr('href')}`,
// category: category
})
.css({ margin: '0 5px 0 0', 'vertical-align': 'middle' })
.insertBefore(a);
});

// Direct download links
if (this.isList) {
$('input[name="nzb"][type="checkbox"]').each((i, el) => {
let checkbox = $(el);
let link = PageUtil.createLink()
.css({ 'margin': '0 0 3px 3px' })
.on('click', (e) => {
e.preventDefault();
link.trigger('nzb.pending');

let nzbId = <string> checkbox.val();

console.info(`[NZB Unity] Adding NZB ${nzbId}`);

return PageUtil.requestAndAddFile(
nzbId,
'',
'http://nzbking.com/nzb/',
{
csrfmiddlewaretoken: this.csrfToken,
nzb: nzbId
}
)
.then((r) => {
setTimeout(() => {
link.trigger(r === false ? 'nzb.failure' : 'nzb.success');
}, 1000);
})
.catch((e) => {
link.trigger('nzb.failure');
console.error(`[NZB Unity] Error fetching NZB content (${nzbId}): ${e.status} ${e.statusText}`);
});

})
.insertAfter(checkbox);
});
}
}
}

$(($) => {
let nzbIntegration = new NZBUnityNzbking();
const nzbIntegration = new NZBUnityNzbking();
});
7 changes: 6 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "NZB Unity",
"version": "1.16.2",
"version": "1.17.0",
"author": "clutterskull@gmail.com",
"description": "Send and control NZB files directly with SABnzbd or NZBGet download clients.",
"background": {
Expand Down Expand Up @@ -53,6 +53,7 @@
{
"matches": [
"*://*.althub.co.za/*",
"*://*.animetosho.org/*",
"*://*.binsearch.info/*",
"*://*.dognzb.cr/*",
"*://*.drunkenslug.com/*",
Expand All @@ -77,6 +78,10 @@
"matches": ["*://*.althub.co.za/*"],
"js": ["content/sites/althub.js"]
},
{
"matches": ["*://*.animetosho.org/*"],
"js": ["content/sites/animetosho.js"]
},
{
"matches": ["*://*.binsearch.info/*"],
"js": ["content/sites/binsearch.js"]
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"src/content/sites/newznab.ts",
"src/content/sites/newznab-detect.ts",
"src/content/sites/althub.ts",
"src/content/sites/animetosho.ts",
"src/content/sites/binsearch.ts",
"src/content/sites/dognzb.ts",
"src/content/sites/drunkenslug.ts",
Expand Down

0 comments on commit 0ac35c3

Please sign in to comment.