1
0
mirror of https://github.com/Kovah/LinkAce.git synced 2025-01-17 13:18:21 +01:00

Add backend fetch endpoint to get html for the html keyword parsing in the frontend (#136)

This commit is contained in:
Kovah 2020-06-18 17:02:59 +02:00
parent e6ded95104
commit c2b513bc28
No known key found for this signature in database
GPG Key ID: AAAA031BA9830D7B
4 changed files with 32 additions and 3 deletions

View File

@ -8,6 +8,8 @@ use App\Models\LinkList;
use App\Models\Tag;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Http;
class FetchController extends Controller
{
@ -78,6 +80,18 @@ class FetchController extends Controller
return response()->json(['linkFound' => $linkCount > 0]);
}
public function htmlForUrl(Request $request): Response
{
$url = $request->input('url');
$response = Http::timeout(3)->get($url);
if ($response->successful()) {
return response($response->body());
}
return response(null);
}
public static function checkForUpdates(): JsonResponse
{
$updateCheck = UpdateHelper::checkForUpdates();

View File

@ -65,9 +65,21 @@ export default class UrlField {
return;
}
fetch(url)
fetch(window.appData.routes.fetch.htmlForUrl, {
method: 'POST',
credentials: 'same-origin',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
_token: window.appData.user.token,
url: url
})
})
.then(response => response.text())
.then(body => this.parseHtmlForKeywords(body));
.then(body => {
if (body !== null) {
this.parseHtmlForKeywords(body)
}
});
}
parseHtmlForKeywords (body) {
@ -77,7 +89,7 @@ export default class UrlField {
if (doc.head.children.length > 0) {
const keywords = doc.head.children.namedItem('keywords');
if (keywords !== null) {
if (keywords !== null && keywords.content.length > 0) {
this.tagSuggestions.displayNewSuggestions(keywords.content.split(','));
}
}

View File

@ -31,6 +31,7 @@
'searchLists' => route('fetch-lists'),
'searchTags' => route('fetch-tags'),
'existingLinks' => route('fetch-existing-links'),
'htmlForUrl' => route('fetch-html-for-url'),
'updateCheck' => route('fetch-update-check'),
'generateApiToken' => route('generate-api-token'),
'generateCronToken' => route('generate-cron-token'),

View File

@ -138,6 +138,8 @@ Route::group(['middleware' => ['auth']], function () {
->name('fetch-lists');
Route::post('fetch/existing-links', [FetchController::class, 'searchExistingUrls'])
->name('fetch-existing-links');
Route::post('fetch/html-for-url', [FetchController::class, 'htmlForUrl'])
->name('fetch-html-for-url');
Route::get('fetch/update-check', [FetchController::class, 'checkForUpdates'])
->name('fetch-update-check');
});