Skip to content

Commit

Permalink
use tabId to fetch actual current page url - fixes #60
Browse files Browse the repository at this point in the history
  • Loading branch information
keithjgrant committed Mar 21, 2018
1 parent f115507 commit ada3f2f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dist/background.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Omnibear</title>
<link rel="stylesheet" href="styles.css">
<script src="index.js"></script>
</head>
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ function handleMessage(request, sender, sendResponse) {
handleBeginAuth(request.payload);
break;
case 'focus-window':
updateFocusedWindow(sender.url, request.payload.selectedEntry);
updateFocusedWindow(
sender.tab.id,
sender.url,
request.payload.selectedEntry
);
break;
case 'select-entry':
selectEntry(request.payload.url);
Expand All @@ -35,8 +39,9 @@ function handleBeginAuth(payload) {
});
}

function updateFocusedWindow(url, selectedEntry) {
function updateFocusedWindow(tabId, url, selectedEntry) {
localStorage.setItem('pageUrl', cleanUrl(url));
localStorage.setItem('pageTabId', tabId);
if (selectedEntry) {
selectEntry(selectedEntry);
} else {
Expand Down
15 changes: 14 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import LoginForm from './LoginForm';
import NoteForm from './form/NoteForm';
import Message from './Message';
import SettingsForm from './settings/SettingsForm';
import {logout} from '../util/utils';
import {logout, getPageUrl} from '../util/utils';

export default class App extends Component {
constructor(props) {
super(props);
this.state = {
pageUrl: '',
};
this.setDefaultView();
}

Expand All @@ -29,6 +32,7 @@ export default class App extends Component {
handleLogout={this.handleLogout}
handleSettings={this.handleSettings}
userFeedback={this.displayMessage}
pageUrl={this.state.pageUrl}
/>
);
}
Expand All @@ -39,6 +43,7 @@ export default class App extends Component {
this.setState({
currentView: 'new-note',
});
this.getPageUrl();
} else {
this.setState({
currentView: 'login',
Expand All @@ -53,6 +58,14 @@ export default class App extends Component {
);
}

getPageUrl() {
getPageUrl().then(url => {
this.setState({
pageUrl: url,
});
});
}

displayMessage = (message, status, location) => {
this.setState({
currentView: 'feedback',
Expand Down
35 changes: 20 additions & 15 deletions src/components/form/NoteForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class NoteForm extends Component {
const draft = getDraft();
this.state = {
postType: this.getPostType(settings),
url: this.getEntryUrl(),
selectedEntry: localStorage.getItem('selectedEntry'),
userDomain: localStorage.getItem('domain'),
entry: draft,
hasSelectedEntry: !!selectedEntry,
Expand All @@ -51,19 +51,21 @@ export default class NoteForm extends Component {
}
}

getEntryUrl() {
const selectedEntry = localStorage.getItem('selectedEntry');
if (selectedEntry) {
return selectedEntry;
} else {
return localStorage.getItem('pageUrl');
getCurrentUrl() {
switch (this.state.postType) {
case NEW_NOTE:
return null;
case PAGE_REPLY:
return this.props.pageUrl;
case ITEM_REPLY:
return this.state.selectedEntry;
break;
}
}

render() {
const {
postType,
url,
isDisabled,
isLoading,
settings,
Expand All @@ -83,7 +85,7 @@ export default class NoteForm extends Component {
/>
<QuickActions
postType={postType}
url={url}
url={this.getCurrentUrl()}
onLike={this.handleLike}
onRepost={this.handleRepost}
onReacji={this.handleReacji}
Expand Down Expand Up @@ -116,12 +118,13 @@ export default class NoteForm extends Component {
}

handleLike = () => {
if (!this.state.url) {
const url = this.getCurrentUrl();
if (!url) {
return;
}
this.postEntry({
h: 'entry',
'like-of': this.state.url,
'like-of': url,
})
.then(location => {
const type = this.state.postType === ITEM_REPLY ? 'Item' : 'Page';
Expand All @@ -134,12 +137,13 @@ export default class NoteForm extends Component {
};

handleRepost = () => {
if (!this.state.url) {
const url = this.getCurrentUrl();
if (!url) {
return;
}
this.postEntry({
h: 'entry',
'repost-of': this.state.url,
'repost-of': url,
})
.then(location => {
const type = this.state.postType === ITEM_REPLY ? 'Item' : 'Page';
Expand All @@ -152,13 +156,14 @@ export default class NoteForm extends Component {
};

handleReacji = emoji => {
if (!this.state.url) {
const url = this.getCurrentUrl();
if (!url) {
return;
}
this.postEntry({
h: 'entry',
content: emoji,
'in-reply-to': this.state.url,
'in-reply-to': url,
})
.then(location => {
const type = this.state.postType === ITEM_REPLY ? 'Item' : 'Page';
Expand Down
9 changes: 9 additions & 0 deletions src/util/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,12 @@ export function generateSlug(content) {
const parts = formatted.split('-');
return parts.splice(0, 6).join('-');
}

export function getPageUrl() {
return new Promise((resolve, reject) => {
var tabId = localStorage.getItem('pageTabId');
chrome.tabs.get(Number(tabId), tab => {
resolve(tab.url);
});
});
}

0 comments on commit ada3f2f

Please sign in to comment.