Skip to content

Commit

Permalink
Fix for plug-in: naver.js (#1296)
Browse files Browse the repository at this point in the history
  • Loading branch information
GrosPoulet committed Feb 11, 2024
1 parent 3293ac0 commit 9a57175
Showing 1 changed file with 162 additions and 8 deletions.
170 changes: 162 additions & 8 deletions plugins/naver.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var hoverZoomPlugins = hoverZoomPlugins || [];
hoverZoomPlugins.push({
name:'naver',
version:'0.1',
version:'0.2',
favicon:'naver.ico',
prepareImgLinks:function (callback) {
const name = this.name;
var res = [];

// sample: https://search.pstatic.net/sunny/?src=https%3A%2F%2Fimage.cnbcfm.com%2Fapi%2Fv1%2Fimage%2F105721511-1549465637499hess.jpg%3Fv%3D1549465603&type=a340
Expand Down Expand Up @@ -92,10 +94,14 @@ hoverZoomPlugins.push({
}

// TV
// sample: https://tv.naver.com/v/22717975/list/67096
// sample: https://tv.naver.com/watch/ELt-oy2EJ8fJ?playlistNo=895649
// sample: https://tv.naver.com/v/22717975/list/67096
// fullsize: https://naver-tvchosun-h.smartmediarep.com/smc/naver/multi/eng/CS1_579016/2f747663686f73756e2f4353322f323032315f707265766965772f656e7465722f433230323130303131375f636c69702f433230323130303131375f636c69705f32303231303933305f32725f31375f7433352e6d7034/0-0-0/content.mp4?solexpire=1633144137&solpathlen=212&soltoken=1b46a469e89697b80c727e5c742fb418&soltokenrule=c29sZXhwaXJlfHNvbHBhdGhsZW58c29sdXVpZA==&soluriver=2&soluuid=9bc4d4bc-a739-49c0-92af-c9c2eb6fa01d&itemtypeid=35&tid=none

$('a[href]:not(.hoverZoomMouseover)').filter(function() { return (/\/v\/\d+/.test(this.href)) }).addClass('hoverZoomMouseover').one('mouseover', function() {
$('a[href]').filter(function() { return ( /\/v\/\d+/.test(this.href) || /\/watch\//.test(this.href) ) }).one('mouseover', function() {
var link = $(this);
var href = this.href;
if (link.data().hoverZoomMouseOver) return;
link.data().hoverZoomMouseOver = true;

hoverZoom.prepareFromDocument($(this), this.href, function(doc, callback) {

Expand All @@ -112,7 +118,6 @@ hoverZoomPlugins.push({
// API call
let requestUrl = 'https://apis.naver.com/rmcnmv/rmcnmv/vod/play/v2.0/' + videoId + '?key=' + inKey;

// still no url found so proceed with API call
// WARNING: CORB error (Cross-Origin Read Blocking) when calling the API from the content script.
// cf https://www.chromium.org/Home/chromium-security/extension-content-script-fetches
// Workaround: call the API from background page.
Expand All @@ -136,10 +141,17 @@ hoverZoomPlugins.push({
});

}, true); // get source async
}).one('mouseleave', function () {
const link = $(this);
link.data().hoverZoomMouseOver = false;
});

// videos
$('a[href*="/vod/"]:not(.hoverZoomMouseover), a[href*="/video?"]:not(.hoverZoomMouseover)').addClass('hoverZoomMouseover').one('mouseover', function() {
// VIDEOS ON DEMAND
$('a[href*="/vod/"], a[href*="/video?"]').one('mouseover', function() {
var link = $(this);
var href = this.href;
if (link.data().hoverZoomMouseOver) return;
link.data().hoverZoomMouseOver = true;

hoverZoom.prepareFromDocument($(this), this.href, function(doc, callback) {

Expand Down Expand Up @@ -184,12 +196,19 @@ hoverZoomPlugins.push({
});

}, true); // get source async
}).one('mouseleave', function () {
const link = $(this);
link.data().hoverZoomMouseOver = false;
});

// MOVIE
// sample: https://ssl.pstatic.net/imgmovie/multimedia/MOVIECLIP/TRAILER/50519_20210923043116.jpg
// fullsize: https://a01-g-naver-vod.pstatic.net/navertv/c/read/v2/VOD_ALPHA/navertv_2021_09_23_655/a1852e80-1c3f-11ec-9639-5ebafcba569f.mp4?__gda__=1633722213_1e76907f42a167a726a85c4382e87879
$('img[src]:not(.hoverZoomMouseover)').filter(function() { return (/\/\d+_\d+\./.test(this.src)) }).addClass('hoverZoomMouseover').one('mouseover', function() {
$('img[src]').filter(function() { return (/\/\d+_\d+\./.test(this.src)) }).one('mouseover', function() {
var link = $(this);
var href = this.href;
if (link.data().hoverZoomMouseOver) return;
link.data().hoverZoomMouseOver = true;

// if no <a> available then use <img> as link
var link = $(this).parents('a')[0];
Expand Down Expand Up @@ -254,6 +273,141 @@ hoverZoomPlugins.push({

} catch { return; }
});
}).one('mouseleave', function () {
const link = $(this);
link.data().hoverZoomMouseOver = false;
});

// LIVE
// sample: https://chzzk.naver.com/live/dec8d19f0bc4be90a4e8b5d57df9c071
// API call: https://api.chzzk.naver.com/service/v2/channels/dec8d19f0bc4be90a4e8b5d57df9c071/live-detail
$('a[href*="/live/"]').one('mouseover', function() {
var link = $(this);
var href = this.href;
if (link.data().hoverZoomMouseOver) return;
link.data().hoverZoomMouseOver = true;

var liveId = undefined;
let m = href.match(/\/live\/([^\/\?]{1,})/);
if (m) {
liveId = m[1];
}

if (liveId == undefined) return;

// reuse previous result
if (link.data().hoverZoomNaverLiveId == liveId) {
if (link.data().hoverZoomNaverLiveSrc) {
link.data().hoverZoomSrc = [link.data().hoverZoomNaverLiveSrc];
link.data().hoverZoomCaption = link.data().hoverZoomNaverLiveCaption;
}
return;
}
link.data().hoverZoomNaverLiveId = liveId;
link.data().hoverZoomNaverLiveSrc = undefined;
link.data().hoverZoomNaverLiveCaption = undefined;

// API call
const requestUrl = `https://api.chzzk.naver.com/service/v2/channels/${liveId}/live-detail`
chrome.runtime.sendMessage({action:'ajaxGet', url:requestUrl}, function (response) {

if (response == null) return;

try {
const data = JSON.parse(response);
if (data == null || data.content == null) return;
const livePlaybackJson = data.content.livePlaybackJson;
if (livePlaybackJson == null) return;
const livePlayback = JSON.parse(livePlaybackJson);
const liveUrl = livePlayback.media[0].path;

link.data().hoverZoomNaverLiveUrl = liveUrl;
link.data().hoverZoomNaverLiveCaption = data.content.liveTitle;
link.data().hoverZoomSrc = [liveUrl];
link.data().hoverZoomCaption = data.content.liveTitle;

callback(link, name);
// Image or video is displayed iff the cursor is still over the link
if (link.data().hoverZoomMouseOver)
hoverZoom.displayPicFromElement(link);
} catch { }
});
}).one('mouseleave', function () {
const link = $(this);
link.data().hoverZoomMouseOver = false;
});

// VIDEOS
// sample: https://chzzk.naver.com/video/107958
// 1st API call: https://api.chzzk.naver.com/service/v2/videos/107958
// 2nd API call: https://apis.naver.com/neonplayer/vodplay/v1/playback/8FCC25CCBDE2BB2EE9E6141B435665847E88?key=V127e6e7c6d2bd024abdc5369a4ac2fb4d639cc34828f9e6edb40c7ecdb9ebaa6e3c35369a4ac2fb4d639
$('a[href]').filter(function() { return ( /\/video\/\d+/.test(this.href) ) }).one('mouseover', function() {
const link = $(this);
const href = this.href;
if (link.data().hoverZoomMouseOver) return;
link.data().hoverZoomMouseOver = true;

const m = href.match(/\/video\/(\d+)/);
if (m == null) return;
const videoId = m[1];

// reuse previous result
if (link.data().hoverZoomNaverVideoId == videoId) {
if (link.data().hoverZoomNaverVideoUrl) link.data().hoverZoomSrc = [link.data().hoverZoomNaverVideoUrl];
return;
}

link.data().hoverZoomNaverVideoId = videoId;
link.data().hoverZoomNaverVideoUrl = undefined;

// clean previous result
link.data().hoverZoomSrc = [];

// API call
const requestUrl1 = `https://api.chzzk.naver.com/service/v2/videos/${videoId}`;

chrome.runtime.sendMessage({action:'ajaxGet', url:requestUrl1}, function (response) {
if (response == null) { return; }
try {
const j1 = JSON.parse(response);
const videoIdEx = j1.content.videoId;
const inKey = j1.content.inKey;
const requestUrl2 = `https://apis.naver.com/neonplayer/vodplay/v1/playback/${videoIdEx}?key=${inKey}`;

chrome.runtime.sendMessage({action:'ajaxGet', url:requestUrl2}, function (response) {
if (response == null) { return; }
try {
const j2 = JSON.parse(response);
let urlMp4 = undefined;
let urlM3u8 = undefined;
const representation = hoverZoom.getKeysInJsonObject(j2, "representation", false);
representation.forEach(function(r) {
r.value.sort(function(a,b) { return (a.width > b.width) ? -1 : ((a.width < b.width) ? 1 : 0) });
const baseURL = r.value[0].baseURL[0]?.value;
const m3u = r.value[0].otherAttributes?.m3u;
if (baseURL.indexOf(".mp4") != -1) urlMp4 = baseURL;
else if (m3u.indexOf(".m3u8") != -1) urlM3u8 = m3u;
});

const bestUrl = urlMp4 ? urlMp4 : urlM3u8;

if (bestUrl) {
link.data().hoverZoomSrc = [bestUrl];
link.data().hoverZoomNaverVideoUrl = bestUrl;
callback(link, name);
// Image is displayed iff the cursor is still over the link
if (link.data().hoverZoomMouseOver)
hoverZoom.displayPicFromElement(link);
}
} catch {}
});

} catch {}
});

}).one('mouseleave', function () {
const link = $(this);
link.data().hoverZoomMouseOver = false;
});

if (res.length) {
Expand Down

0 comments on commit 9a57175

Please sign in to comment.