Home » Javascript » javascript – Make cookie for Params in HTML

javascript – Make cookie for Params in HTML

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have a cookie code for JWplayer that creates a resume play back cookie the .js is
https://cdn.jsdelivr.net/npm/[email protected]/src/js.cookie.min.js

    /**
 * Minified by jsDelivr using Terser v3.14.1.
 * Original file: /npm/[email protected]/src/js.cookie.js
 * 
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
!function(e){var n;if("function"==typeof define&&define.amd&&(define(e),n=!0),"object"==typeof exports&&(module.exports=e(),n=!0),!n){var t=window.Cookies,o=window.Cookies=e();o.noConflict=function(){return window.Cookies=t,o}}}(function(){function e(){for(var e=0,n={};e<arguments.length;e++){var t=arguments[e];for(var o in t)n[o]=t[o]}return n}function n(e){return e.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}return function t(o){function r(){}function i(n,t,i){if("undefined"!=typeof document){"number"==typeof(i=e({path:"/"},r.defaults,i)).expires&&(i.expires=new Date(1*new Date+864e5*i.expires)),i.expires=i.expires?i.expires.toUTCString():"";try{var c=JSON.stringify(t);/^[\{\[]/.test(c)&&(t=c)}catch(e){}t=o.write?o.write(t,n):encodeURIComponent(String(t)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),n=encodeURIComponent(String(n)).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var f="";for(var u in i)i[u]&&(f+="; "+u,!0!==i[u]&&(f+="="+i[u].split(";")[0]));return document.cookie=n+"="+t+f}}function c(e,t){if("undefined"!=typeof document){for(var r={},i=document.cookie?document.cookie.split("; "):[],c=0;c<i.length;c++){var f=i[c].split("="),u=f.slice(1).join("=");t||'"'!==u.charAt(0)||(u=u.slice(1,-1));try{var a=n(f[0]);if(u=(o.read||o)(u,a)||n(u),t)try{u=JSON.parse(u)}catch(e){}if(r[a]=u,e===a)break}catch(e){}}return e?r[e]:r}}return r.set=i,r.get=function(e){return c(e,!1)},r.getJSON=function(e){return c(e,!0)},r.remove=function(n,t){i(n,"",e(t,{expires:-1}))},r.defaults={},r.withConverter=t,r}(function(){})});
//# sourceMappingURL=/sm/b0ce608ffc029736e9ac80a8dd6a7db2da8e1d45d2dcfc92043deb2214aa30d8.map

And it works… But heres my question i use 1 .php page to call the mp4 file via ID Params
Example

DOmain.com/video.php?id=blahblah

and then i call different videos by changing “blahblah”

DOmain.com/video.php?id=blahblah1
DOmain.com/video.php?id=blahblah2

Okay so here is my issue…… So if im watching Video Blahblah1 and stop it on 22:34 and then i go to blahblah2 it starts IT on 22:34…. Instead of at the beginning it like carries the cookie video to video even tho the id and .mp4 changes i imagine cuz its all coming from

DOmain.com/video.php?id=

So my question is what would i need to edit in that code so it pays attention that the ?id=IDNUMBER is different and NOT just the same video.php file?

Any advice with this would be amazing thanks in advance!

EXTRA INFO ALL PARTS OF THE SAID CODE

Js.Cookie.min.js

 /**
     * Minified by jsDelivr using Terser v3.14.1.
     * Original file: /npm/[email protected]/src/js.cookie.js
     * 
     * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
     */
    !function(e){var n;if("function"==typeof define&&define.amd&&(define(e),n=!0),"object"==typeof exports&&(module.exports=e(),n=!0),!n){var t=window.Cookies,o=window.Cookies=e();o.noConflict=function(){return window.Cookies=t,o}}}(function(){function e(){for(var e=0,n={};e<arguments.length;e++){var t=arguments[e];for(var o in t)n[o]=t[o]}return n}function n(e){return e.replace(/(%[0-9A-Z]{2})+/g,decodeURIComponent)}return function t(o){function r(){}function i(n,t,i){if("undefined"!=typeof document){"number"==typeof(i=e({path:"/"},r.defaults,i)).expires&&(i.expires=new Date(1*new Date+864e5*i.expires)),i.expires=i.expires?i.expires.toUTCString():"";try{var c=JSON.stringify(t);/^[\{\[]/.test(c)&&(t=c)}catch(e){}t=o.write?o.write(t,n):encodeURIComponent(String(t)).replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g,decodeURIComponent),n=encodeURIComponent(String(n)).replace(/%(23|24|26|2B|5E|60|7C)/g,decodeURIComponent).replace(/[\(\)]/g,escape);var f="";for(var u in i)i[u]&&(f+="; "+u,!0!==i[u]&&(f+="="+i[u].split(";")[0]));return document.cookie=n+"="+t+f}}function c(e,t){if("undefined"!=typeof document){for(var r={},i=document.cookie?document.cookie.split("; "):[],c=0;c<i.length;c++){var f=i[c].split("="),u=f.slice(1).join("=");t||'"'!==u.charAt(0)||(u=u.slice(1,-1));try{var a=n(f[0]);if(u=(o.read||o)(u,a)||n(u),t)try{u=JSON.parse(u)}catch(e){}if(r[a]=u,e===a)break}catch(e){}}return e?r[e]:r}}return r.set=i,r.get=function(e){return c(e,!1)},r.getJSON=function(e){return c(e,!0)},r.remove=function(n,t){i(n,"",e(t,{expires:-1}))},r.defaults={},r.withConverter=t,r}(function(){})});
    //# sourceMappingURL=/sm/b0ce608ffc029736e9ac80a8dd6a7db2da8e1d45d2dcfc92043deb2214aa30d8.map

Logger.js

function Logger(id) {
  function _logger() {
    this.element = document.getElementById(id);
    return this;
  };

  _logger.prototype = {
    clear() {
      this.element.textContent = '';
    },

    log(message) {
      this.element.textContent = [this.element.textContent, message].filter(Boolean).join('\n');
    }
  };

  return new _logger();
};

then the player code insert to run it

// this looks for cookie with 'resumevideodata' 

playerInstance.once('play', function() {
  let cookieData = Cookies.get('resumevideodata');
  if (!cookieData) {
    console.log('No video resume cookie detected. Refresh page.');
    return;
  }
  const [resumeAt, duration] = cookieData.split(':');

  if (resumeAt < duration) {
    if (confirm("Resume Playback?")) { // result is a boolean indicating whether OK (true) or Cancel (false) was selected.
      playerInstance.seek(resumeAt); // resume playback from position saved in cookie
      return;
    } else { // false
      playerInstance.seek(0); // playback from start
      return;
    }
  }
});

// this sets a cookie with 'resumevideodata'

playerInstance.on('time', function(e) {
  Cookies.set('resumevideodata', `${Math.floor(e.position)}:${playerInstance.getDuration()}`);
});
How to&Answers: