<commit_before>import config from '../../config.json'

const {
    clientId,
    authUri,
    redirectUri,
    host
} = config.dev.auth

const scope = ['email', 'public_profile', 'user_about_me', 'user_posts']
const url = `https://www.facebook.com/dialog/oauth?client_id=${clientId}&redirect_uri=${redirectUri}&auth_type=rerequest&scope=${scope.join(',')}`

function getQueryString(url, field) {
    const reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
    const string = reg.exec(url);
    return string ? string[1] : null;
}

export default function callback(originTabIndex, sendResponse) {
    let authTabId

    chrome.tabs.create({
            url: url,
            selected: true
        }, (tab) => {
            authTabId = tab.id;
            chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
                    if (
                        tabId == authTabId &&
                        changeInfo.status == 'complete' &&
                        tab.url.startsWith(redirectUri)
                    ) {
                        const code = getQueryString(tab.url, 'code')
                        const options = {
                            method: 'POST',
                            headers: {
                                "Content-Type": "application/json"
                            },
                            mode: 'cors',
                            body: JSON.stringify({code})

                        }
                        fetch(authUri, options)
                            .then(response => response.json())
                            .then(data => {
                                console.log('data===>', data);
                                sendResponse({data})
                                chrome.tabs.highlight({
                                    'tabs': originTabIndex
                                }, () => chrome.tabs.remove(tabId))
                            })

                    }
            })

    })
}<commit_msg>refactor(auth): split to smaller functions<commit_after>import config from '../../config.json'

const {
    clientId,
    authUri,
    redirectUri,
    host
} = config.dev.auth

const scope = [
    'email', 'public_profile', 'user_about_me', 
    'user_posts', 'user_tagged_places'
]
const url = `https://www.facebook.com/dialog/oauth?client_id=${clientId}&redirect_uri=${redirectUri}&auth_type=rerequest&scope=${scope.join(',')}`

function getQueryString(url, field) {
    const reg = new RegExp('[?&]' + field + '=([^&#]*)', 'i');
    const string = reg.exec(url);
    return string ? string[1] : null;
}

function doneFetchCode(authTabId, tabId, changeInfo, tab) {
    return (
        tabId == authTabId &&
        changeInfo.status == 'complete' &&
        tab.url.startsWith(redirectUri)
    )
}


function tabUpdateHandler(authTabId, originTabIndex, sendResponse) {
    return (tabId, changeInfo, tab) => {
        if (doneFetchCode(authTabId, tabId, changeInfo, tab)) {
            const code = getQueryString(tab.url, 'code')
            const options = {
                method: 'POST',
                headers: {
                    "Content-Type": "application/json"
                },
                mode: 'cors',
                body: JSON.stringify({
                    code
                })
            }

            fetch(authUri, options)
                .then(response => response.json())
                .then(data => {
                    sendResponse({data})
                    chrome.tabs.highlight({
                        'tabs': originTabIndex
                    }, () => chrome.tabs.remove(tabId))

                })

        }
    }
}

export default function callback(originTabIndex, sendResponse) {
    let authTabId

    chrome.tabs.create({
        url: url,
        selected: true
    }, (tab) => {
        authTabId = tab.id;
        chrome.tabs.onUpdated.addListener(tabUpdateHandler(authTabId, originTabIndex, sendResponse))
    })
}
{'fraction_non_alphanumeric': 0.13066465256797583, 'fraction_numerical': 0.0005035246727089627, 'mean_word_length': 1.487789605510332, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2342936', 'n_tokens_mistral': 1090, 'n_tokens_neox': 1039, 'n_words': 194}