Skip to content

Commit

Permalink
add: search-query-types
Browse files Browse the repository at this point in the history
  • Loading branch information
Metacor committed Jul 3, 2023
1 parent 1544f5f commit b83b62e
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 42 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
![](src/assets/sites.png)
![](https://i.imgur.com/MVR68qM.jpg)
## What the Extension Does
- Adds a "Search on Nyaa" Button to Anime and Manga pages
- Sites: **[MyAnimeList](https://i.imgur.com/1hymaOS.png), [AniList](https://i.imgur.com/DtNugQF.png), [Kitsu](https://i.imgur.com/TVKqRcK.png), [Anime-Planet](https://i.imgur.com/zohAYbs.png), [AnimeNewsNetwork](https://i.imgur.com/xOYS17r.png), [AniDB](https://i.imgur.com/pRDcUVh.png)**
- Search Parameters can be changed and saved in the [Extension Popup window](https://i.imgur.com/oiu0DZW.png)
- Search Parameters can be changed and saved in the [Extension Popup window](https://i.imgur.com/bzaChNf.png)
- For Manga pages, the Category setting will search for the "Literature" equivalent
- All tabs with a supported website currently open will automatically refresh on Save
- The Search will include both the Japanese(Romaji) & English titles, if they exist and are different
- as well as the base titles of each if the titles include: ("Season"|"Part"|": "|" - ")
- for example, clicking the button on [Shingeki no Kyojin Season 3 Part 2](https://myanimelist.net/anime/38524/Shingeki_no_Kyojin_Season_3_Part_2) will return the search query:
- "Shingeki no Kyojin Season 3 Part 2"|"Attack on Titan Season 3 Part 2"|"Shingeki no Kyojin"|"Attack on Titan"

- By default the search will include both the Japanese(Romaji) & English titles — if they exist, and they are different
- the search will also add the base titles, if they include: ("Season"|"Part"|": "|" - ")
- for example, clicking the button on [Shingeki no Kyojin Season 3 Part 2](https://myanimelist.net/anime/38524/Shingeki_no_Kyojin_Season_3_Part_2) will return the search query:
- _"Shingeki no Kyojin Season 3 Part 2"|"Attack on Titan Season 3 Part 2"|"Shingeki no Kyojin"|"Attack on Titan"_
- _**additional "Query" types are available**: the Default combines both "Exact" and "Base"._
- Fuzzy: Searches for the site's default title only, without quotes ~ allows fuzzy matching
- Exact: Japanese and English full titles ~ searches for exact title names as written
- Base: Japanese and English base titles ~ searches with Seasons and Parts removed

# Firefox - [Extension Page](https://addons.mozilla.org/en-US/firefox/addon/nyaa-linker/)

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "nyaa-linker",
"version": "1.0.1",
"version": "1.1.0",
"description": "Adds a button to Anime and Manga database websites that opens a relevant Nyaa search",
"scripts": {
"zip": "cd src && zip -r nyaa-linker.zip . -x manifest-*.json && cd ..",
"firefox": "mv src/manifest-firefox.json src/manifest.json && npm run zip && mv src/manifest.json src/manifest-firefox.json",
"chrome": "mv src/manifest-chrome.json src/manifest.json && npm run zip && mv src/manifest.json src/manifest-chrome.json"
"zip": "npm run firefox && npm run chrome",
"firefox": "mv src/manifest-firefox.json src/manifest.json && cd src && zip -r nyaa-linker-%npm_package_version%-%npm_lifecycle_event%.zip . -x manifest-*.json && cd .. && mv src/manifest.json src/manifest-firefox.json",
"chrome": "mv src/manifest-chrome.json src/manifest.json && cd src && zip -r nyaa-linker-%npm_package_version%-%npm_lifecycle_event%.zip . -x manifest-*.json && cd .. && mv src/manifest.json src/manifest-chrome.json"
},
"devDependencies": {
"eslint": "^8.26.0",
Expand All @@ -26,5 +26,5 @@
"printWidth": 150
},
"author": "Metacor <Metacor.Code@gmail.com> (https://github.com/Metacor/nyaa-linker)",
"license": "GPLv3"
"license": "GPL-3.0-only"
}
Binary file removed src/assets/sites.png
Binary file not shown.
1 change: 1 addition & 0 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const defaultSettings = () => {
settings: {
filter_setting: '0',
category_setting: '1_2',
query_setting: 'default',
sort_setting: 'seeders',
order_setting: 'desc',
},
Expand Down
83 changes: 56 additions & 27 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const searchNyaa = () => {
chrome.storage.sync.get('settings', (load) => {
const filter = load.settings.filter_setting;
let category = load.settings.category_setting;
const queryType = load.settings.query_setting;
const sort = load.settings.sort_setting;
const order = load.settings.order_setting;

Expand Down Expand Up @@ -166,52 +167,80 @@ const searchNyaa = () => {

awaitLoadOf('.nyaaBtn').then(() => {
let query = `"${titleJap}"|"${titleEng}"`;
let baseJap = titleJap;
let baseEng = titleEng;

if (!titleEng || titleJap.toLowerCase() === titleEng.toLowerCase()) {
query = titleJap;
} else {
const getBase = () => {
const hasSeason = /(?<![\w])(season)(?![\w])/i;
const hasNum = /(?<![\w])[0-9]+(?:st|[nr]d|th)(?![\w])/i;
const hasWord = /(?<![\w])(first|second|third|fourth|fifth|(the final|final))(?![\w])/i;
const hasPart = /(?<![\w])(part)(?![\w])/i;

if (titleJap.includes(': ') || titleJap.includes(' - ')) {
titleJap.includes(': ') && (query += `|"${titleJap.split(': ').shift()}"`);
titleJap.includes(' - ') && (query += `|"${titleJap.split(' - ').pop()}"`);
if (baseJap.includes(': ') || baseJap.includes(' - ')) {
baseJap.includes(': ') && (baseJap = baseJap.split(': ').shift());
baseJap.includes(' - ') && (baseJap = baseJap.split(' - ').pop());
} else {
if (hasSeason.test(titleJap)) {
if (hasNum.test(titleJap) || hasWord.test(titleJap)) {
if (hasSeason.test(baseJap)) {
if (hasNum.test(baseJap) || hasWord.test(baseJap)) {
let japNum, japWord;
hasNum.test(titleJap) && (japNum = titleJap.match(hasNum)[0]);
hasWord.test(titleJap) && (japWord = titleJap.match(hasWord)[0]);
japNum && (query += `|"${titleJap.split(` ${japNum}`).shift()}"`);
japWord && (query += `|"${titleJap.split(` ${japWord}`).shift()}"`);
hasNum.test(baseJap) && (japNum = baseJap.match(hasNum)[0]);
hasWord.test(baseJap) && (japWord = baseJap.match(hasWord)[0]);
japNum && (baseJap = baseJap.split(` ${japNum}`).shift());
japWord && (baseJap = baseJap.split(` ${japWord}`).shift());
} else {
query += `|"${titleJap.split(/( season)/i).shift()}"`;
baseJap = baseJap.split(/( season)/i).shift();
}
} else if (hasPart.test(titleJap)) {
query += `|"${titleJap.split(/( part)/i).shift()}"`;
} else if (hasPart.test(baseJap)) {
baseJap = baseJap.split(/( part)/i).shift();
}
}

if (titleEng.includes(': ') || titleEng.includes(' - ')) {
titleEng.includes(': ') && (query += `|"${titleEng.split(': ').shift()}"`);
titleEng.includes(' - ') && (query += `|"${titleEng.split(' - ').pop()}"`);
if (baseEng.includes(': ') || baseEng.includes(' - ')) {
baseEng.includes(': ') && (baseEng = baseEng.split(': ').shift());
baseEng.includes(' - ') && (baseEng = baseEng.split(' - ').pop());
} else {
if (hasSeason.test(titleEng)) {
if (hasNum.test(titleEng) || hasWord.test(titleEng)) {
if (hasSeason.test(baseEng)) {
if (hasNum.test(baseEng) || hasWord.test(baseEng)) {
let engNum, engWord;
hasNum.test(titleEng) && (engNum = titleEng.match(hasNum)[0]);
hasWord.test(titleEng) && (engWord = titleEng.match(hasWord)[0]);
engNum && (query += `|"${titleEng.split(` ${engNum}`).shift()}"`);
engWord && (query += `|"${titleEng.split(` ${engWord}`).shift()}"`);
hasNum.test(baseEng) && (engNum = baseEng.match(hasNum)[0]);
hasWord.test(baseEng) && (engWord = baseEng.match(hasWord)[0]);
engNum && (baseEng = baseEng.split(` ${engNum}`).shift());
engWord && (baseEng = baseEng.split(` ${engWord}`).shift());
} else {
query += `|"${titleEng.split(/( season)/i).shift()}"`;
baseEng = baseEng.split(/( season)/i).shift();
}
} else if (hasPart.test(titleEng)) {
query += `|"${titleEng.split(/( part)/i).shift()}"`;
} else if (hasPart.test(baseEng)) {
baseEng = baseEng.split(/( part)/i).shift();
}
}
};

if (!titleEng || titleJap.toLowerCase() === titleEng.toLowerCase()) {
query = titleJap;
} else {
getBase();

switch (queryType) {
case 'default':
if (baseJap == titleJap && baseEng == titleEng) {
break;
} else {
query = `"${titleJap}"|"${titleEng}"|"${baseJap}"|"${baseEng}"`;
}
break;
case 'fuzzy':
query = titleJap;
break;
case 'exact':
break;
case 'base':
if (baseJap == baseEng) {
break;
} else {
query = `"${baseJap}"|"${baseEng}"`;
}
break;
}
}

if (btn) {
Expand Down
2 changes: 1 addition & 1 deletion src/manifest-chrome.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Nyaa Linker",
"version": "1.0.1",
"version": "1.1.0",
"description": "Adds a button to Anime and Manga database websites that opens a relevant Nyaa search",
"manifest_version": 3,

Expand Down
2 changes: 1 addition & 1 deletion src/manifest-firefox.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Nyaa Linker",
"version": "1.0.1",
"version": "1.1.0",
"description": "Adds a button to Anime and Manga database websites that opens a relevant Nyaa search",
"manifest_version": 2,

Expand Down
13 changes: 11 additions & 2 deletions src/popup/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@
<option value="1_4">Raw</option>
</select>

<label for="sort_select">Sort Type:</label>
<label for="query_select">Query:</label>
<select id="query_select">
<option value="default" title="Creates a search using both the 'Exact' and 'Base' options">Default</option>
<option value="fuzzy" title="Searches for the site's default title only, without quotes — allows fuzzy matching">Fuzzy</option>
<option value="exact" title="Japanese and English full titles — searches for exact title names as written">Exact</option>
<option value="base" title="Japanese and English base titles — searches with Seasons and Parts removed">Base</option>
</select>

<label for="sort_select">Sort:</label>
<select id="sort_select">
<option value="comments">Comments</option>
<option value="size">Size</option>
Expand All @@ -36,11 +44,12 @@
<option value="downloads">Downloads</option>
</select>

<label for="order_select">Sort Order:</label>
<label for="order_select">Order:</label>
<select id="order_select">
<option value="desc">Descending</option>
<option value="asc">Ascending</option>
</select>

</div>
<button id="save">Save</button>
</body>
Expand Down
2 changes: 2 additions & 0 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ window.onload = () => {
if (load.settings) {
document.getElementById('filter_select').value = load.settings.filter_setting;
document.getElementById('category_select').value = load.settings.category_setting;
document.getElementById('query_select').value = load.settings.query_setting;
document.getElementById('sort_select').value = load.settings.sort_setting;
document.getElementById('order_select').value = load.settings.order_setting;
}
Expand All @@ -13,6 +14,7 @@ const saveSettings = () => {
const settings = {};
settings['filter_setting'] = document.getElementById('filter_select').value;
settings['category_setting'] = document.getElementById('category_select').value;
settings['query_setting'] = document.getElementById('query_select').value;
settings['sort_setting'] = document.getElementById('sort_select').value;
settings['order_setting'] = document.getElementById('order_select').value;
return settings;
Expand Down

0 comments on commit b83b62e

Please sign in to comment.