diff --git a/js/hoverzoom.js b/js/hoverzoom.js index 9267a6a0b..f6da5bb30 100644 --- a/js/hoverzoom.js +++ b/js/hoverzoom.js @@ -3087,6 +3087,138 @@ var hoverZoom = { cTimeEnd(msg); }, + + // Find key(s) in JSON object and return corresponding value(s) and path(s) + // If key not found then return [] + // Search is NOT case-sensitive + // https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0 + getKeysInJsonObject:function (jsonObj, searchKey, isRegex, maxDeepLevel, currDeepLevel) { + + var bShowInfo = false; + + maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100; + currDeepLevel = currDeepLevel ? currDeepLevel : 1 ; + isRegex = isRegex ? isRegex : false; + + // check RegEx validity if needed + var re; + if (isRegex) { + try { + re = new RegExp(searchKey); + } catch (e) { + cLog(e); + return []; + } + } + + if ( currDeepLevel > maxDeepLevel ) { + return []; + } else { + + var keys = []; + + for ( var curr in jsonObj ) { + var currElem = jsonObj[curr]; + + if ( currDeepLevel == 1 && bShowInfo ) { cLog("getKeysInJsonObject : Looking property \"" + curr + "\" ") } + + if ( isRegex ? re.test(curr) : curr.toLowerCase() === searchKey.toLowerCase() ){ + var r = {}; + r.key = curr; + r.value = currElem; + r.path = '["' + curr + '"]'; + r.depth = currDeepLevel; + keys.push( r ); + } + + if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof" + // search again :D + var deepKeys = hoverZoom.getKeysInJsonObject( currElem, searchKey, isRegex, maxDeepLevel, currDeepLevel + 1 ); + + for ( var e = 0; e < deepKeys.length; e++ ) { + // update path backwards + deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path; + keys.push( deepKeys[e] ); + } + } + } + return keys; + } + }, + + // Find value(s) in JSON object and return corresponding key(s) and path(s) + // If value not found then return [] + // Search is NOT case-sensitive + // ref: https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0 + getValuesInJsonObject:function (jsonObj, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel) { + + var bShowInfo = false; + + maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100; + currDeepLevel = currDeepLevel ? currDeepLevel : 1 ; + isRegex = isRegex ? isRegex : false; + + // check RegEx validity if needed + var re; + if ( isRegex ) { + try { + re = new RegExp(searchValue); + } catch (e) { + cLog(e); + return []; + } + } else { + searchValue = searchValue.toString().toLowerCase(); + } + + if ( currDeepLevel > maxDeepLevel ) { + return []; + } else { + + var keys = []; + + for ( var curr in jsonObj ) { + var currElem = jsonObj[curr]; + + if ( currDeepLevel == 1 && bShowInfo ) { cLog("getValuesInJsonObject : Looking property \"" + curr + "\" ") } + + if ( typeof currElem == "undefined" ) continue; + + if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof" + // search again :D + var deepKeys = hoverZoom.getValuesInJsonObject( currElem, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel + 1 ); + for ( var e = 0; e < deepKeys.length; e++ ) { + // update path backwards + deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path; + keys.push( deepKeys[e] ); + } + } else { + + if ( isRegex ? re.test(currElem) : ( isPartialMatch ? currElem.toString().toLowerCase().indexOf(searchValue) != -1 : currElem.toString().toLowerCase() === searchValue ) ){ + + var r = {}; + r.key = curr; + r.value = currElem; + r.path = '["' + curr + '"]'; + r.depth = currDeepLevel; + keys.push( r ); + if (isFirstMatchOnly) return keys; + } + } + } + return keys; + } + }, + + // Return JSON object corresponding to path, without using the Evil eval + // path syntax: [key1][key2][key3]... + getJsonObjectFromPath:function (jsonObj, path) { + if (!path || path.length < 4) return jsonObj; + const keys = path.substr(2, path.length - 4).split('"]["'); + let result = jsonObj; + keys.forEach(key => result = result[key]); + return result; + } }; hoverZoom.loadHoverZoom(); diff --git a/plugins/startpage.js b/plugins/startpage.js index 5b6b3d4fb..2e3f3a7a1 100644 --- a/plugins/startpage.js +++ b/plugins/startpage.js @@ -27,134 +27,6 @@ hoverZoomPlugins.push({ return undefined; } - // Find key(s) in JSON object and return corresponding value(s) and path(s) - // If key not found then return [] - // Search is NOT case-sensitive - // https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0 - function getKeysInJsonObject(jsonObj, searchKey, isRegex, maxDeepLevel, currDeepLevel) { - - var bShowInfo = false; - - maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100; - currDeepLevel = currDeepLevel ? currDeepLevel : 1 ; - isRegex = isRegex ? isRegex : false; - - // check RegEx validity if needed - var re; - if (isRegex) { - try { - re = new RegExp(searchKey); - } catch (e) { - cLog(e); - return []; - } - } - - if ( currDeepLevel > maxDeepLevel ) { - return []; - } else { - - var keys = []; - - for ( var curr in jsonObj ) { - var currElem = jsonObj[curr]; - - if ( currDeepLevel == 1 && bShowInfo ) { cLog("getKeysInJsonObject : Looking property \"" + curr + "\" ") } - - if ( isRegex ? re.test(curr) : curr.toLowerCase() === searchKey.toLowerCase() ){ - var r = {}; - r.key = curr; - r.value = currElem; - r.path = '["' + curr + '"]'; - r.depth = currDeepLevel; - keys.push( r ); - } - - if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof" - // search again :D - var deepKeys = getKeysInJsonObject( currElem, searchKey, isRegex, maxDeepLevel, currDeepLevel + 1 ); - - for ( var e = 0; e < deepKeys.length; e++ ) { - // update path backwards - deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path; - keys.push( deepKeys[e] ); - } - } - } - return keys; - } - } - - // Find value(s) in JSON object and return corresponding key(s) and path(s) - // If value not found then return [] - // Search is NOT case-sensitive - // ref: https://gist.github.com/killants/569c4af5f2983e340512916e15a48ac0 - function getValuesInJsonObject(jsonObj, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel) { - - var bShowInfo = false; - - maxDeepLevel = ( maxDeepLevel || maxDeepLevel == 0 ) ? maxDeepLevel : 100; - currDeepLevel = currDeepLevel ? currDeepLevel : 1 ; - isRegex = isRegex ? isRegex : false; - - // check RegEx validity if needed - var re; - if ( isRegex ) { - try { - re = new RegExp(searchValue); - } catch (e) { - cLog(e); - return []; - } - } else { - searchValue = searchValue.toString().toLowerCase(); - } - - if ( currDeepLevel > maxDeepLevel ) { - return []; - } else { - - var keys = []; - - for ( var curr in jsonObj ) { - var currElem = jsonObj[curr]; - - if ( currDeepLevel == 1 && bShowInfo ) { cLog("getValuesInJsonObject : Looking property \"" + curr + "\" ") } - - if ( typeof currElem == "undefined" ) continue; - - if ( typeof currElem == "object" ) { // object is "object" and "array" is also in the eyes of "typeof" - // search again :D - var deepKeys = getValuesInJsonObject( currElem, searchValue, isRegex, isPartialMatch, isFirstMatchOnly, maxDeepLevel, currDeepLevel + 1 ); - for ( var e = 0; e < deepKeys.length; e++ ) { - // update path backwards - deepKeys[e].path = '["' + curr + '"]' + deepKeys[e].path; - keys.push( deepKeys[e] ); - } - } else { - - if ( isRegex ? re.test(currElem) : ( isPartialMatch ? currElem.toString().toLowerCase().indexOf(searchValue) != -1 : currElem.toString().toLowerCase() === searchValue ) ){ - - var r = {}; - r.key = curr; - r.value = currElem; - r.path = '["' + curr + '"]'; - r.depth = currDeepLevel; - keys.push( r ); - if (isFirstMatchOnly) return keys; - } - } - } - return keys; - } - } - - // Return JSON object corresponding to path, without using the Evil eval - // path syntax: [key1][key2][key3]... - function getJsonObjectFromPath(path) { - return new Function('return ' + spJson + path)(); - } - //$('link[href*="proxy-image"]:not(.hoverZoomFetched)').addClass('hoverZoomFetched').each(function () { $('img[src*="proxy-image"]:not(.hoverZoomFetched)').addClass('hoverZoomFetched').each(function () { let link = $(this); @@ -163,9 +35,9 @@ hoverZoomPlugins.push({ // search for thumbnail url among sp data if (spJson.indexOf(src) == -1) return; - let values = getValuesInJsonObject(spData, src, false, true, true); // look for a partial match & stop after 1st match + let values = hoverZoom.getValuesInJsonObject(spData, src, false, true, true); // look for a partial match & stop after 1st match if (values.length == 0) return; - let o = getJsonObjectFromPath(values[0].path.substring(0, values[0].path.lastIndexOf('['))); + let o = hoverZoom.getJsonObjectFromPath(spData, values[0].path.substring(0, values[0].path.lastIndexOf('['))); // extract fullsize url from Object let fullsizeUrl = o.clickUrl || o.anonImageViewUrl; fullsizeUrl = fullsizeUrl.replace(/.*piurl=(.*)&sp=.*/, '$1');