From 8822595f325c53e4a716357926c6408140f9e768 Mon Sep 17 00:00:00 2001 From: BilawalAhmed0900 Date: Mon, 13 Jul 2020 15:15:25 +0500 Subject: [PATCH] Make new socket for every download to avoid socket expiration --- background.js | 16 +++++++++++++--- manifest.json | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/background.js b/background.js index 4b10afd..909a631 100644 --- a/background.js +++ b/background.js @@ -1,7 +1,7 @@ const sUsrAg = window.navigator.userAgent; const isFirefox = (sUsrAg.indexOf("Chrome") === -1); const browserVar = (!isFirefox) ? chrome : browser; -const socket = new WebSocket("ws://127.0.0.1:49152"); +var socket = null; const toDownloadMime = [ "application/f4m+xml", "application/gzip", @@ -59,7 +59,13 @@ function sendUsingCookies(downloadItem, cookies, isAudio, isVideo, isExecutable) "referrer": downloadItem.referrer || "", "fileSize": downloadItem.fileSize, "mime": downloadItem.mime, "cookies": cookiesString, "youtubeLink": false, "isAudio": isAudio, "isVideo": isVideo, "isExecutable": isExecutable, "userAgent": sUsrAg}); - socket.send(toBeSent); + let socket = new WebSocket("ws://127.0.0.1:49152"); + socket.addEventListener("open", (e) => + { + socket.send(toBeSent); + socket.close(); + socket = null; + }); } function updater_function(downloadItem) @@ -74,7 +80,7 @@ function updater_function(downloadItem) const audioFound = (audioRegex.exec(downloadItem.mime) != null); const videoFound = (videoRegex.exec(downloadItem.mime) != null); - if ((socket.readyState === 1 && downloadItem.url !== "" && downloadItem.url.indexOf("ftp://") === -1 && downloadItem.url.indexOf("blob:") === -1) && + if ((downloadItem.url !== "" && downloadItem.url.indexOf("ftp://") === -1 && downloadItem.url.indexOf("blob:") === -1) && (toDownloadMime.indexOf(downloadItem.mime) > -1 || audioFound || videoFound)) { browserVar.downloads.cancel(downloadItem.id); @@ -156,3 +162,7 @@ if (!isFirefox) }); } +window.addEventListener('online', (e) => +{ + socket = new WebSocket("ws://127.0.0.1:49152"); +}); diff --git a/manifest.json b/manifest.json index d24cc55..979f318 100644 --- a/manifest.json +++ b/manifest.json @@ -1,6 +1,6 @@ { "name": "LinkDownloader Extension", - "version": "1.7.5", + "version": "1.8.0", "description": "Extension for LinkDownloader!", "permissions": [ "cookies",