<commit_before>// Used only to remember last song title
var clipTitle = '';

// Glabal constant for the song container ....
var CONTAINER_SELECTOR = '.GEKKVSQBL-';
var TRACK_SELECTOR = '.GEKKVSQBN-';
var ARTIST_SELECTOR = '.GEKKVSQBAY';
var ALBUM_SELECTOR = '.GEKKVSQBF-';
var DURATION_SELECTOR = '#PlayerDuration';

$(function(){
	$(CONTAINER_SELECTOR).live('DOMSubtreeModified', function(e) {
		updateNowPlaying();
		return;
	});

	$(DURATION_SELECTOR).live('DOMSubtreeModified', function(e) {
		updateNowPlaying();
		return;
	});

	//setup unload handler
	$(window).unload(function() {
		chrome.runtime.sendMessage({type: 'reset'});
		return true;      
	});
});

/**
 * Called every time we load a new song
 */
function updateNowPlaying(){
    var parsedInfo = parseInfo();
    artist   = parsedInfo['artist']; 	//global
    track    = parsedInfo['track'];	//global
    album    = parsedInfo['album'];
    duration = parsedInfo['duration']; 	//global

    if (artist == '' || track == '' || duration == 0) {return;}

    // check if the same track is being played and we have been called again
    // if the same track is being played we return
    if (clipTitle == track) {
		return;
    }
    clipTitle = track;

    chrome.runtime.sendMessage({type: 'validate', artist: artist, track: track}, function(response) {
      if (response != false) {
		console.log("Scrobbling information: artist: " + artist + ", track: " + track + ", album: " + album + ", duration: " + duration);
        chrome.extension.sendMessage({type: 'nowPlaying', artist: artist, track: track, album: album, duration: duration});
      }
      // on failure send nowPlaying 'unknown song'
      else {
        chrome.runtime.sendMessage({type: 'nowPlaying', duration: duration});
      }
    });
}

function parseInfo() {
    var artist   = '';
    var track    = '';
    var album    = '';
    var duration = 0;

    // Get artist and song names
    var trackValue = $(TRACK_SELECTOR).children().first().attr("title");
    var artistValue = $(ARTIST_SELECTOR).children().first().attr("title");
    var albumValue = $(ALBUM_SELECTOR).children().first().attr("title");
    var durationValue = $(DURATION_SELECTOR).text();

    try {
        if (null != artistValue) {
            artist = artistValue.replace(/^\s+|\s+$/g,'');
        }
        if (null != trackValue) {
            track = trackValue.replace(/^\s+|\s+$/g,'');
        }
        if (null != albumValue) {
            album = albumValue.replace(/^\s+|\s+$/g,'');
        }
        if (null != durationValue) {
            duration = parseDuration(durationValue);
        }
    } catch(err) {
        return {artist: '', track: '', duration: 0};
    }

    return {artist: artist, track: track, album: album, duration: duration};
}

function parseDuration(artistTitle) {
	try {
		match = artistTitle.match(/\d+:\d+/g)[0]

		mins    = match.substring(0, match.indexOf(':'));
		seconds = match.substring(match.indexOf(':')+1);
		return parseInt(mins*60) + parseInt(seconds);
	} catch(err){
		return 0;
	}
}

/**
 * Simply request the scrobbler.js to submit song previusly specified by calling updateNowPlaying()
 */
/*function scrobbleTrack() {
   // stats<commit_msg>Attempted to fix duration bug... Not fixed<commit_after>// Used only to remember last song title
var currentTrack = '';
var currentDuration = '';

// Glabal constant for the song container ....
var CONTAINER_SELECTOR = '.GEKKVSQBL-';
var TRACK_SELECTOR = '.GEKKVSQBN-';
var ARTIST_SELECTOR = '.GEKKVSQBAY';
var ALBUM_SELECTOR = '.GEKKVSQBF-';
var DURATION_SELECTOR = '#PlayerDuration';

$(function(){
	$(CONTAINER_SELECTOR).live('DOMSubtreeModified', function(e) {
		if (!newTrack()) {
			return;
		}
		updateNowPlaying();
		return;
	});

	$(DURATION_SELECTOR).live('DOMSubtreeModified', function(e) {
		if (!newTrack()) {
			return;
		}
		updateNowPlaying();
		return;
	});

	//setup unload handler
	$(window).unload(function() {
		chrome.runtime.sendMessage({type: 'reset'});
		return true;      
	});
});

/**
 * Called every time we load a new song
 */
function updateNowPlaying(){
    var parsedInfo = parseInfo();
    artist   = parsedInfo['artist']; 	//global
    track    = parsedInfo['track'];	//global
    album    = parsedInfo['album'];
    duration = parsedInfo['duration']; 	//global

    if (artist == '' || track == '' || duration == 0) {return;}

    chrome.runtime.sendMessage({type: 'validate', artist: artist, track: track}, function(response) {
      if (response != false) {
		console.log("Scrobbling track: artist: " + artist + ", track: " + track + ", album: " + album + ", duration: " + duration);
        chrome.extension.sendMessage({type: 'nowPlaying', artist: artist, track: track, album: album, duration: duration});
      }
      // on failure send nowPlaying 'unknown song'
      else {
        chrome.runtime.sendMessage({type: 'nowPlaying', duration: duration});
      }
    });
}

function parseInfo() {
    var artist   = '';
    var track    = '';
    var album    = '';
    var duration = 0;

    // Get artist and song names
    var rawArtistValue = $(ARTIST_SELECTOR).children().first().attr("title");
    var rawTrackValue = $(TRACK_SELECTOR).children().first().attr("title");
    var rawAlbumValue = $(ALBUM_SELECTOR).children().first().attr("title");
    var rawDurationValue = $(DURATION_SELECTOR).text();

    try {
        if (null != rawArtistValue) {
            artist = rawArtistValue.trim();
        }
        if (null != rawTrackValue) {
            track = rawTrackValue.trim();
        }
        if (null != rawAlbumValue) {
            album = rawAlbumValue.trim();
        }
        if (null != rawDurationValue) {
            duration = parseDuration(rawDurationValue);
        }
    } catch(err) {
        return {artist: '', track: '', duration: 0};
    }

    return {artist: artist, track: track, album: album, duration: duration};
}

function parseDuration(artistTitle) {
	try {
		match = artistTitle.match(/\d+:\d+/g)[0]

		mins    = match.substring(0, match.indexOf(':'));
		seconds = match.substring(match.indexOf(':')+1);
		return parseInt(mins*60) + parseInt(seconds);
	} catch(err){
		return 0;
	}
}

function newTrack() {
    var rawTrackValue = $(TRACK_SELECTOR).children().first().attr("title");
    var rawDurationValue = $(DURATION_SELECTOR).text();
	if (currentTrack == rawTrackValue || currentDuration == rawDurationValue) {
		return false;
	}
	currentTrack = rawTrackValue;
	currentDuration = rawDurationValue
	return true;
}

/**
 * Simply request the scrobbler.js to submit song previusly specified by calling updateNowPlaying()
 */
/*function scrobbleTrack() {
   // stats
{'fraction_non_alphanumeric': 0.1614598978672274, 'fraction_numerical': 0.0027035145689396217, 'mean_word_length': 3.161875, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '897376', 'n_tokens_mistral': 2128, 'n_tokens_neox': 2029, 'n_words': 491}