<commit_before>'use strict';

var element = document.getElementById( 'steamdb-extension-protip' );

if( element )
{
	element.setAttribute( 'hidden', true );
}

GetOption( { 'steamdb-highlight': true, 'steamdb-hide-not-interested': false }, function( items )
{
	if( !items[ 'steamdb-highlight' ] )
	{
		return;
	}
	
	var apps     = document.querySelectorAll( 'tr.app' ),
	    packages = document.querySelectorAll( 'tr.package' ),
	    packageScope = document.querySelector( '.scope-package' ),
	    appScope = document.querySelector( '.scope-app' );
	
	if( apps.length > 0 || packages.length > 0 || appScope || packageScope )
	{
		var xhr = new XMLHttpRequest();
		
		var cache = localStorage.getItem( 'userdata.cached' );
		
		xhr.open( 'GET', 'https://store.steampowered.com/dynamicstore/userdata/?_=' + cache, true );
		xhr.responseType = 'json';
		
		xhr.onerror = function()
		{
			localStorage.setItem( 'userdata.cached', Date.now() );
			
			var id = document.createElement( 'div' );
			id.className = 'extension-warning';
			
			var icon = document.createElement( 'span' );
			icon.className = 'mega-octicon octicon-squirrel';
			
			id.appendChild( icon );
			id.appendChild( document.createTextNode( 'Failed to load game data from Steam store due to a network failure.' ) );
			
			document.body.appendChild( id );
		};
		
		xhr.onreadystatechange = function()
		{
			if( xhr.readyState !== 4 || xhr.status !== 200 )
			{
				return;
			}
			
			var data = xhr.response, id, i, mapAppsToElements = [], mapPackagesToElements = [];
			
			if( !data.rgOwnedPackages.length )
			{
				localStorage.setItem( 'userdata.cached', Date.now() );
				
				id = document.createElement( 'a' );
				id.className = 'extension-warning';
				id.href = 'https://store.steampowered.com/login/';
				
				var icon = document.createElement( 'span' );
				icon.className = 'mega-octicon octicon-hubot';
				
				id.appendChild( icon );
				id.appendChild( document.createTextNode( 'You are not logged in on Steam Store, highlighting will not work.' ) );
				
				document.body.appendChild( id );
				
				return;
			}
			
			for( i = 0; i < apps.length; i++ )
			{
				element = apps[ i ];<commit_msg>Add a close button to hide "not logged in" alert, cache successful requests so that cached data can be used when user is not logged in<commit_after>'use strict';

var element = document.getElementById( 'steamdb-extension-protip' );

if( element )
{
	element.setAttribute( 'hidden', true );
}

GetOption( { 'steamdb-highlight': true, 'steamdb-hide-not-interested': false }, function( items )
{
	if( !items[ 'steamdb-highlight' ] )
	{
		return;
	}
	
	var apps     = document.querySelectorAll( 'tr.app' ),
	    packages = document.querySelectorAll( 'tr.package' ),
	    packageScope = document.querySelector( '.scope-package' ),
	    appScope = document.querySelector( '.scope-app' );
	
	if( apps.length > 0 || packages.length > 0 || appScope || packageScope )
	{
		var xhr = new XMLHttpRequest();
		
		function AddCloseButton( element )
		{
			var x = document.createElement( 'span' );
			x.className = 'octicon octicon-x';
			
			var link = document.createElement( 'a' );
			link.className = 'pull-right tooltipped tooltipped-nw';
			link.href = '#';
			link.style.color = '#FFF';
			link.setAttribute( 'aria-label', 'Do not show this message again' );
			link.appendChild( x );
			
			link.addEventListener( 'click', function( e )
			{
				e.preventDefault();
				
				this.parentNode.parentNode.removeChild( this.parentNode );
				
				localStorage.setItem( 'userdata.warning.hidden', 'true' );
			} );
			
			element.appendChild( link );
		};
		
		var cache = localStorage.getItem( 'userdata.cached' );
		
		xhr.open( 'GET', 'https://store.steampowered.com/dynamicstore/userdata/?_=' + cache, true );
		xhr.responseType = 'json';
		
		xhr.onerror = function()
		{
			TryToUseCachedData( );
			
			localStorage.setItem( 'userdata.cached', Date.now() );
			
			if( localStorage.getItem( 'userdata.warning.hidden' ) === 'true' )
			{
				return;
			}
			
			var id = document.createElement( 'div' );
			id.className = 'extension-warning';
			
			AddCloseButton( id );
			
			var icon = document.createElement( 'span' );
			icon.className = 'mega-octicon octicon-squirrel';
			
			id.appendChild( icon );
			id.appendChild( document.createTextNode( 'Failed to load game data from Steam store due to a network failure.' ) );
			
			document.body.appendChild( id );
		};
		
		xhr.onreadystatechange = function()
		{
			if( xhr.readyState !== 4 || xhr.status !== 200 )
			{
				return;
			}
			
			if( !xhr.response.rgOwnedPackages.length )
			{
				TryToUseCachedData( );
				
				localStorage.setItem( 'userdata.cached', Date.now() );
				
				if( localStorage.getItem( 'userdata.warning.hidden' ) === 'true' )
				{
					return;
				}
				
				var id = document.createElement( 'a' );
				id.className = 'extension-warning';
				id.href = 'https://store.steampowered.com/login/';
				
				AddCloseButton( id );
				
				var icon = document.createElement( 'span' );
				icon.className = 'mega-octicon octicon-hubot';
				
				id.appendChild( icon );
				id.appendChild( document.createTextNode( 'You are not logged in on Steam Store, owned game highlighting will not work.' ) );
				
				document.body.appendChild( id );
				
				return;
			}
			
			OnDataLoaded( xhr.response );
			
			// TODO: This shouldn't be executed if browser cache was hit
			if( typeof chrome !== 'undefined' )
			{
				chrome.storage.local.set( { 'userdata.stored': JSON.stringify( xhr.response ) } );
			}
		};
		
		function TryToUseCachedData( )
		{
			chrome.storage.local.get( 'userdata.stored', function( data )
			{
				if( data[ 'userdata.stored' ] )
				{
					OnDataLoaded( JSON.parse( data[ 'userdata.stored' ] ) );
				}
			} );
		}
		
		function OnDataLoaded( data )
		{
			var id, i, mapAppsToElements = [], mapPackagesToElements = [];
			
			for( i = 0; i < apps.length; i++ )
			{
				element = apps[ i ];
{'fraction_non_alphanumeric': 0.14663099816084266, 'fraction_numerical': 0.002340745694699883, 'mean_word_length': 2.953734302709848, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7138426', 'n_tokens_mistral': 2349, 'n_tokens_neox': 1899, 'n_words': 433}