Skip to content

Commit

Permalink
automate authorization refresh cycle in Chrome version (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulgb committed Feb 3, 2019
1 parent 24e5c95 commit d9d96fd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
46 changes: 36 additions & 10 deletions src/background/common.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
export let matchTweetURL = 'https?://(?:mobile\\.)?twitter.com/(.+)/status/(\\d+)'
export let matchTweetURLRegex = new RegExp(matchTweetURL)

// Stores auth and CSRF tokens once they are captured in the headers.
let auth = {
csrfToken: null,
authorization: null
}

// If we have to refresh the page to gather the headers, store the tab and
// tweet to load after we get the headers in this object.
let waiting = {
tabId: null,
tweetId: null
}

export function onMessageFromContentScript(request) {
if (request.message === 'share') {
// Handle share button click. The payload is the tree structure.

fetch('https://1l8hy2eaaj.execute-api.us-east-1.amazonaws.com/default/treeverse_post', {
method: 'POST',
body: JSON.stringify(request.payload),
Expand All @@ -27,6 +37,14 @@ export function updateAuth(headers) {
auth.authorization = header.value
}
}

if (waiting.tabId !== null) {
// If we previously reloaded the page in order to capture the tokens,
// initiate Treeverse now.
injectScripts(waiting.tabId, waiting.tweetId)
waiting.tabId = null
waiting.tweetId = null
}
}

export function getTweetFromURL(url: string): string {
Expand All @@ -36,20 +54,28 @@ export function getTweetFromURL(url: string): string {
}
}

export function clickAction(tab) {
let tweetId = getTweetFromURL(tab.url)
export function injectScripts(tabId: number, tweetId: string) {
var indexUrl = chrome.extension.getURL('resources')

if (auth.authorization === null) {
alert('Couldn’t capture authorization key, try refreshing Twitter and attempting again.')
return
}

chrome.tabs.executeScript(tab.id, {
chrome.tabs.executeScript(tabId, {
file: 'resources/script/viewer.js'
}, () => {
chrome.tabs.executeScript(tab.id, {
chrome.tabs.executeScript(tabId, {
code: `Treeverse.initialize(${JSON.stringify(indexUrl)}, ${JSON.stringify(tweetId)}, ${JSON.stringify(auth)});`
})
})
}

export function clickAction(tab) {
let tweetId = getTweetFromURL(tab.url)
let tabId = tab.id

if (auth.authorization === null) {
// If authorization hasn't been captured yet, we need to reload
// the page to do so.
waiting.tabId = tabId
waiting.tweetId = tweetId
chrome.tabs.reload(tab.id)
} else {
injectScripts(tabId, tweetId)
}
}
2 changes: 2 additions & 0 deletions src/viewer/tweet_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ export namespace TweetParser {
tweet.time = new Date(entry.created_at).getTime()
tweet.replies = entry.reply_count

console.log(entry, tweet);

tweets.push(tweet)
}
return tweets
Expand Down

0 comments on commit d9d96fd

Please sign in to comment.