{"action":"created","comment":{"url":"https://api.github.com/repos/ornament5/spotify-extension/pulls/comments/214402642","pull_request_review_id":150705932,"id":214402642,"node_id":"MDI0OlB1bGxSZXF1ZXN0UmV2aWV3Q29tbWVudDIxNDQwMjY0Mg==","diff_hunk":"@@ -0,0 +1,169 @@\n+// -----------------Event Listeners--------------- //\n+window.addEventListener(`duration`, handler);\n+\n+window.addEventListener(`load`, handler);\n+\n+chrome.runtime.onMessage.addListener(\n+  function (request) {\n+    // listen for tab updated messages sent from background.js\n+    if (request.message === `done`) {\n+      handler();\n+    }\n+  });\n+\n+\n+function handler() {\n+  if (window.location.href.includes(\"https://open.spotify.com/collection/playlists\")) {\n+    setTimeout(() => go(), 500); // Zasto Timeout? Ako se direktno otvori stranica sa plejlistama, prvi put ne stigne da pokupi podatke pa bude NaNh:NaNm:NaNs \n+  }\n+}\n+\n+/* Main function that inserts each playlist's duration if the token is still valid OR \n+   requests & stores a new token, if the current one is expired, and subsequently inserts total durations\n+*/\n+function go() {\n+  if (token.isActive()) {\n+    takeCareOfBusiness();\n+  } else {\n+    if (window.location.href.includes(`access_token`)) {\n+      let tokenObject = token.extract(window.location.href);\n+      appStorage.set(`token`, tokenObject);\n+      takeCareOfBusiness();\n+    } else {\n+      token.request();\n+    }\n+  }\n+}\n+\n+function takeCareOfBusiness() {\n+  playlistIds.setAll();\n+  playlistDurations.getAll();\n+  setTimeout(() => playlistDurations.renderAll(), 500);\n+}\n+\n+// -----------------Token Functions--------------- //\n+const token = {\n+  request() {\n+    let requestURL = `https://accounts.spotify.com/authorize?client_id=4033df41b69c46598a007e72e87448a1&redirect_uri=https://open.spotify.com/collection/playlists&scope=playlist-read-private%20playlist-read-collaborative&response_type=token`\n+    window.location.href = requestURL;\n+  },\n+  extract(urlString) {\n+    let tokenId = urlString.match(/access_token=(.+?)&/) ? urlString.match(/access_token=(.+?)&/)[1] : null,\n+      tokenDuration = urlString.match(/expires_in=(.+?)$/) ? urlString.match(/expires_in=(.+?)$/)[1] : null,\n+      expirationTime = tokenDuration ? Date.now() + Number(tokenDuration) * 1000 : 0;\n+    return {\n+      id: tokenId,\n+      expires: expirationTime\n+    }\n+  },\n+  isExpired(expirationTime) {\n+    return Date.now() + 1000 > expirationTime;\n+  },\n+  isActive() {\n+    return appStorage.get(`token`) && !this.isExpired(appStorage.get(`token`).expires);\n+  }\n+\n+};\n+\n+// -----------------Storage Util Functions--------------- //\n+const appStorage = {\n+  set(name, data) {\n+    localStorage.setItem(name, JSON.stringify(data));\n+  },\n+  get(name) {\n+    if (!localStorage.getItem(name)) {\n+      return null;\n+    }\n+    let storedData = localStorage.getItem(name);\n+    return JSON.parse(storedData);\n+  }\n+};\n+\n+// -----------------Time Util Functions--------------- //\n+const util = {\n+  trackTimeAdder(tracks) {\n+    let totalDuration = 0;\n+    for (let trackObject of tracks.items) {\n+      totalDuration += Math.floor(trackObject.track.duration_ms / 1000);\n+    }\n+    return totalDuration;\n+  },\n+\n+  timeFormatter(timeInSecs) {\n+    let hours = Math.floor(timeInSecs / 3600),\n+      mins = Math.floor((timeInSecs - hours * 3600) / 60),\n+      secs = timeInSecs - (hours * 3600) - (mins * 60);\n+    let hoursString = hours < 10 ? `0` + hours : `` + hours,\n+      minsString = mins < 10 ? `0` + mins : `` + mins,\n+      secsString = secs < 10 ? `0` + secs : `` + secs;\n+    return hoursString + `h:` + minsString + `m:` + secsString + `s`;\n+  }\n+};\n+\n+// -----------------PlaylistIDs Functions for DOM Nodes--------------- //\n+const playlistIds = {\n+  getSingle(playlistNode) {\n+    let playlistUrl = playlistNode.href,\n+      playlistId = playlistUrl.match(/playlist\\/(.+)/)[1];\n+    return playlistId;\n+  },\n+  getAll(playlistsCollection) {\n+    let playlists = {};\n+    for (let i = 0; i < playlistsCollection.length; i++) {\n+      let playlistId = this.getSingle(playlistsCollection[i]);\n+      playlists[playlistId] = \"\";\n+    }\n+    return playlists;\n+  },\n+  setAll() {\n+    let playlistsCollection = document.querySelectorAll(\".mo-info-name\"),\n+      playlistsData = this.getAll(playlistsCollection);\n+    appStorage.set(\"playlists\", playlistsData);\n+  }\n+};\n+\n+// -----------------Playlist Duration Functions with Spotify API calls--------------- //\n+const playlistDurations = {\n+  getSingle(playlistId, accessToken) {\n+    let xhr = new XMLHttpRequest(),\n+      requestURL = `https://api.spotify.com/v1/playlists/${playlistId}/tracks?fields=items(track(duration_ms))`\n+    self = this;\n+    xhr.open(\"GET\", requestURL);\n+    xhr.setRequestHeader(`Authorization`, `Bearer ${accessToken}`);\n+    xhr.onreadystatechange = function () {\n+      if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {\n+        self.setSingle(playlistId, JSON.parse(xhr.responseText));\n+      }\n+    }\n+    xhr.send();\n+  },\n+  getAll() {\n+    let token = appStorage.get(\"token\").id,\n+      allPlaylistIds = Object.keys(appStorage.get(\"playlists\"));\n+    for (let playlistId of allPlaylistIds) {","path":"listDurationOutside.js","position":143,"original_position":143,"commit_id":"1e55fef13281b94b09e027051f340a0aa6aaa601","original_commit_id":"1e55fef13281b94b09e027051f340a0aa6aaa601","user":{"login":"petarfrontend","id":42075180,"node_id":"MDQ6VXNlcjQyMDc1MTgw","avatar_url":"https://avatars2.githubusercontent.com/u/42075180?v=4","gravatar_id":"","url":"https://api.github.com/users/petarfrontend","html_url":"https://github.com/petarfrontend","followers_url":"https://api.github.com/users/petarfrontend/followers","following_url":"https://api.github.com/users/petarfrontend/following{/other_user}","gists_url":"https://api.github.com/users/petarfrontend/gists{/gist_id}","starred_url":"https://api.github.com/users/petarfrontend/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petarfrontend/subscriptions","organizations_url":"https://api.github.com/users/petarfrontend/orgs","repos_url":"https://api.github.com/users/petarfrontend/repos","events_url":"https://api.github.com/users/petarfrontend/events{/privacy}","received_events_url":"https://api.github.com/users/petarfrontend/received_events","type":"User","site_admin":false},"body":"use `const`","created_at":"2018-08-31T16:07:00Z","updated_at":"2018-08-31T16:12:27Z","html_url":"https://github.com/ornament5/spotify-extension/pull/1#discussion_r214402642","pull_request_url":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1","author_association":"COLLABORATOR","_links":{"self":{"href":"https://api.github.com/repos/ornament5/spotify-extension/pulls/comments/214402642"},"html":{"href":"https://github.com/ornament5/spotify-extension/pull/1#discussion_r214402642"},"pull_request":{"href":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1","id":211846578,"node_id":"MDExOlB1bGxSZXF1ZXN0MjExODQ2NTc4","html_url":"https://github.com/ornament5/spotify-extension/pull/1","diff_url":"https://github.com/ornament5/spotify-extension/pull/1.diff","patch_url":"https://github.com/ornament5/spotify-extension/pull/1.patch","issue_url":"https://api.github.com/repos/ornament5/spotify-extension/issues/1","number":1,"state":"open","locked":false,"title":"Renamed the rest of the methods and conten scripts","user":{"login":"ornament5","id":25712201,"node_id":"MDQ6VXNlcjI1NzEyMjAx","avatar_url":"https://avatars3.githubusercontent.com/u/25712201?v=4","gravatar_id":"","url":"https://api.github.com/users/ornament5","html_url":"https://github.com/ornament5","followers_url":"https://api.github.com/users/ornament5/followers","following_url":"https://api.github.com/users/ornament5/following{/other_user}","gists_url":"https://api.github.com/users/ornament5/gists{/gist_id}","starred_url":"https://api.github.com/users/ornament5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ornament5/subscriptions","organizations_url":"https://api.github.com/users/ornament5/orgs","repos_url":"https://api.github.com/users/ornament5/repos","events_url":"https://api.github.com/users/ornament5/events{/privacy}","received_events_url":"https://api.github.com/users/ornament5/received_events","type":"User","site_admin":false},"body":"Renamed the rest of the methods, reorganized the code, and renamed the content scripts, so that is clear what feature their code is for.","created_at":"2018-08-29T18:30:51Z","updated_at":"2018-08-31T16:12:26Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d8894571e72f5342713d99ecca7ce8a7cfc28025","assignee":null,"assignees":[],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1/commits","review_comments_url":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1/comments","review_comment_url":"https://api.github.com/repos/ornament5/spotify-extension/pulls/comments{/number}","comments_url":"https://api.github.com/repos/ornament5/spotify-extension/issues/1/comments","statuses_url":"https://api.github.com/repos/ornament5/spotify-extension/statuses/1e55fef13281b94b09e027051f340a0aa6aaa601","head":{"label":"ornament5:feature-total-duration","ref":"feature-total-duration","sha":"1e55fef13281b94b09e027051f340a0aa6aaa601","user":{"login":"ornament5","id":25712201,"node_id":"MDQ6VXNlcjI1NzEyMjAx","avatar_url":"https://avatars3.githubusercontent.com/u/25712201?v=4","gravatar_id":"","url":"https://api.github.com/users/ornament5","html_url":"https://github.com/ornament5","followers_url":"https://api.github.com/users/ornament5/followers","following_url":"https://api.github.com/users/ornament5/following{/other_user}","gists_url":"https://api.github.com/users/ornament5/gists{/gist_id}","starred_url":"https://api.github.com/users/ornament5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ornament5/subscriptions","organizations_url":"https://api.github.com/users/ornament5/orgs","repos_url":"https://api.github.com/users/ornament5/repos","events_url":"https://api.github.com/users/ornament5/events{/privacy}","received_events_url":"https://api.github.com/users/ornament5/received_events","type":"User","site_admin":false},"repo":{"id":146341981,"node_id":"MDEwOlJlcG9zaXRvcnkxNDYzNDE5ODE=","name":"spotify-extension","full_name":"ornament5/spotify-extension","owner":{"login":"ornament5","id":25712201,"node_id":"MDQ6VXNlcjI1NzEyMjAx","avatar_url":"https://avatars3.githubusercontent.com/u/25712201?v=4","gravatar_id":"","url":"https://api.github.com/users/ornament5","html_url":"https://github.com/ornament5","followers_url":"https://api.github.com/users/ornament5/followers","following_url":"https://api.github.com/users/ornament5/following{/other_user}","gists_url":"https://api.github.com/users/ornament5/gists{/gist_id}","starred_url":"https://api.github.com/users/ornament5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ornament5/subscriptions","organizations_url":"https://api.github.com/users/ornament5/orgs","repos_url":"https://api.github.com/users/ornament5/repos","events_url":"https://api.github.com/users/ornament5/events{/privacy}","received_events_url":"https://api.github.com/users/ornament5/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ornament5/spotify-extension","description":"Chrome extension that provides some additional features to the online player, like total playlist duration","fork":false,"url":"https://api.github.com/repos/ornament5/spotify-extension","forks_url":"https://api.github.com/repos/ornament5/spotify-extension/forks","keys_url":"https://api.github.com/repos/ornament5/spotify-extension/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ornament5/spotify-extension/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ornament5/spotify-extension/teams","hooks_url":"https://api.github.com/repos/ornament5/spotify-extension/hooks","issue_events_url":"https://api.github.com/repos/ornament5/spotify-extension/issues/events{/number}","events_url":"https://api.github.com/repos/ornament5/spotify-extension/events","assignees_url":"https://api.github.com/repos/ornament5/spotify-extension/assignees{/user}","branches_url":"https://api.github.com/repos/ornament5/spotify-extension/branches{/branch}","tags_url":"https://api.github.com/repos/ornament5/spotify-extension/tags","blobs_url":"https://api.github.com/repos/ornament5/spotify-extension/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ornament5/spotify-extension/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ornament5/spotify-extension/git/refs{/sha}","trees_url":"https://api.github.com/repos/ornament5/spotify-extension/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ornament5/spotify-extension/statuses/{sha}","languages_url":"https://api.github.com/repos/ornament5/spotify-extension/languages","stargazers_url":"https://api.github.com/repos/ornament5/spotify-extension/stargazers","contributors_url":"https://api.github.com/repos/ornament5/spotify-extension/contributors","subscribers_url":"https://api.github.com/repos/ornament5/spotify-extension/subscribers","subscription_url":"https://api.github.com/repos/ornament5/spotify-extension/subscription","commits_url":"https://api.github.com/repos/ornament5/spotify-extension/commits{/sha}","git_commits_url":"https://api.github.com/repos/ornament5/spotify-extension/git/commits{/sha}","comments_url":"https://api.github.com/repos/ornament5/spotify-extension/comments{/number}","issue_comment_url":"https://api.github.com/repos/ornament5/spotify-extension/issues/comments{/number}","contents_url":"https://api.github.com/repos/ornament5/spotify-extension/contents/{+path}","compare_url":"https://api.github.com/repos/ornament5/spotify-extension/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ornament5/spotify-extension/merges","archive_url":"https://api.github.com/repos/ornament5/spotify-extension/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ornament5/spotify-extension/downloads","issues_url":"https://api.github.com/repos/ornament5/spotify-extension/issues{/number}","pulls_url":"https://api.github.com/repos/ornament5/spotify-extension/pulls{/number}","milestones_url":"https://api.github.com/repos/ornament5/spotify-extension/milestones{/number}","notifications_url":"https://api.github.com/repos/ornament5/spotify-extension/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ornament5/spotify-extension/labels{/name}","releases_url":"https://api.github.com/repos/ornament5/spotify-extension/releases{/id}","deployments_url":"https://api.github.com/repos/ornament5/spotify-extension/deployments","created_at":"2018-08-27T19:07:50Z","updated_at":"2018-08-29T17:12:14Z","pushed_at":"2018-08-29T18:30:51Z","git_url":"git://github.com/ornament5/spotify-extension.git","ssh_url":"git@github.com:ornament5/spotify-extension.git","clone_url":"https://github.com/ornament5/spotify-extension.git","svn_url":"https://github.com/ornament5/spotify-extension","homepage":null,"size":13,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"ornament5:master","ref":"master","sha":"ee0bbb01b6c877f51f3982f0394fde2369b30957","user":{"login":"ornament5","id":25712201,"node_id":"MDQ6VXNlcjI1NzEyMjAx","avatar_url":"https://avatars3.githubusercontent.com/u/25712201?v=4","gravatar_id":"","url":"https://api.github.com/users/ornament5","html_url":"https://github.com/ornament5","followers_url":"https://api.github.com/users/ornament5/followers","following_url":"https://api.github.com/users/ornament5/following{/other_user}","gists_url":"https://api.github.com/users/ornament5/gists{/gist_id}","starred_url":"https://api.github.com/users/ornament5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ornament5/subscriptions","organizations_url":"https://api.github.com/users/ornament5/orgs","repos_url":"https://api.github.com/users/ornament5/repos","events_url":"https://api.github.com/users/ornament5/events{/privacy}","received_events_url":"https://api.github.com/users/ornament5/received_events","type":"User","site_admin":false},"repo":{"id":146341981,"node_id":"MDEwOlJlcG9zaXRvcnkxNDYzNDE5ODE=","name":"spotify-extension","full_name":"ornament5/spotify-extension","owner":{"login":"ornament5","id":25712201,"node_id":"MDQ6VXNlcjI1NzEyMjAx","avatar_url":"https://avatars3.githubusercontent.com/u/25712201?v=4","gravatar_id":"","url":"https://api.github.com/users/ornament5","html_url":"https://github.com/ornament5","followers_url":"https://api.github.com/users/ornament5/followers","following_url":"https://api.github.com/users/ornament5/following{/other_user}","gists_url":"https://api.github.com/users/ornament5/gists{/gist_id}","starred_url":"https://api.github.com/users/ornament5/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ornament5/subscriptions","organizations_url":"https://api.github.com/users/ornament5/orgs","repos_url":"https://api.github.com/users/ornament5/repos","events_url":"https://api.github.com/users/ornament5/events{/privacy}","received_events_url":"https://api.github.com/users/ornament5/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/ornament5/spotify-extension","description":"Chrome extension that provides some additional features to the online player, like total playlist duration","fork":false,"url":"https://api.github.com/repos/ornament5/spotify-extension","forks_url":"https://api.github.com/repos/ornament5/spotify-extension/forks","keys_url":"https://api.github.com/repos/ornament5/spotify-extension/keys{/key_id}","collaborators_url":"https://api.github.com/repos/ornament5/spotify-extension/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/ornament5/spotify-extension/teams","hooks_url":"https://api.github.com/repos/ornament5/spotify-extension/hooks","issue_events_url":"https://api.github.com/repos/ornament5/spotify-extension/issues/events{/number}","events_url":"https://api.github.com/repos/ornament5/spotify-extension/events","assignees_url":"https://api.github.com/repos/ornament5/spotify-extension/assignees{/user}","branches_url":"https://api.github.com/repos/ornament5/spotify-extension/branches{/branch}","tags_url":"https://api.github.com/repos/ornament5/spotify-extension/tags","blobs_url":"https://api.github.com/repos/ornament5/spotify-extension/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/ornament5/spotify-extension/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/ornament5/spotify-extension/git/refs{/sha}","trees_url":"https://api.github.com/repos/ornament5/spotify-extension/git/trees{/sha}","statuses_url":"https://api.github.com/repos/ornament5/spotify-extension/statuses/{sha}","languages_url":"https://api.github.com/repos/ornament5/spotify-extension/languages","stargazers_url":"https://api.github.com/repos/ornament5/spotify-extension/stargazers","contributors_url":"https://api.github.com/repos/ornament5/spotify-extension/contributors","subscribers_url":"https://api.github.com/repos/ornament5/spotify-extension/subscribers","subscription_url":"https://api.github.com/repos/ornament5/spotify-extension/subscription","commits_url":"https://api.github.com/repos/ornament5/spotify-extension/commits{/sha}","git_commits_url":"https://api.github.com/repos/ornament5/spotify-extension/git/commits{/sha}","comments_url":"https://api.github.com/repos/ornament5/spotify-extension/comments{/number}","issue_comment_url":"https://api.github.com/repos/ornament5/spotify-extension/issues/comments{/number}","contents_url":"https://api.github.com/repos/ornament5/spotify-extension/contents/{+path}","compare_url":"https://api.github.com/repos/ornament5/spotify-extension/compare/{base}...{head}","merges_url":"https://api.github.com/repos/ornament5/spotify-extension/merges","archive_url":"https://api.github.com/repos/ornament5/spotify-extension/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/ornament5/spotify-extension/downloads","issues_url":"https://api.github.com/repos/ornament5/spotify-extension/issues{/number}","pulls_url":"https://api.github.com/repos/ornament5/spotify-extension/pulls{/number}","milestones_url":"https://api.github.com/repos/ornament5/spotify-extension/milestones{/number}","notifications_url":"https://api.github.com/repos/ornament5/spotify-extension/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/ornament5/spotify-extension/labels{/name}","releases_url":"https://api.github.com/repos/ornament5/spotify-extension/releases{/id}","deployments_url":"https://api.github.com/repos/ornament5/spotify-extension/deployments","created_at":"2018-08-27T19:07:50Z","updated_at":"2018-08-29T17:12:14Z","pushed_at":"2018-08-29T18:30:51Z","git_url":"git://github.com/ornament5/spotify-extension.git","ssh_url":"git@github.com:ornament5/spotify-extension.git","clone_url":"https://github.com/ornament5/spotify-extension.git","svn_url":"https://github.com/ornament5/spotify-extension","homepage":null,"size":13,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":1,"license":null,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1"},"html":{"href":"https://github.com/ornament5/spotify-extension/pull/1"},"issue":{"href":"https://api.github.com/repos/ornament5/spotify-extension/issues/1"},"comments":{"href":"https://api.github.com/repos/ornament5/spotify-extension/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/ornament5/spotify-extension/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/ornament5/spotify-extension/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/ornament5/spotify-extension/statuses/1e55fef13281b94b09e027051f340a0aa6aaa601"}},"author_association":"OWNER"}}
{"actor":{"display_login":"petarfrontend"}}