Skip to content

Commit

Permalink
add storage for most recently used string
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyvetter committed Feb 1, 2017
1 parent 4259294 commit 6ce3714
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
41 changes: 28 additions & 13 deletions src/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,21 +131,36 @@ class Popup extends React.Component {
this.setState({out, inLang, outLang});
}
);

chrome.storage.local.set({latestRefmtString: value});
}
}

document.addEventListener('DOMContentLoaded', () => {
chrome.tabs.executeScript(
{code: 'window.getSelection().toString();'},
(selection) => {
// in ff you get a single result, chrome returns an array of results
selection = Array.isArray(selection)
? selection[0]
: selection;
ReactDOM.render(
<Popup initialSelectedText={selection}/>,
document.getElementById('app'),
);
},
);
Promise.all([
new Promise((resolve) =>
chrome.tabs.executeScript(
{code: 'window.getSelection().toString();'},
resolve,
)
),
new Promise((resolve) =>
chrome.storage.local.get(
'latestRefmtString',
({latestRefmtString: selection}) => resolve(selection),
)
),
]).then(([selection, latestRefmtString]) => {
// in ff you get a single result, chrome returns an array of results
selection = Array.isArray(selection)
? selection[0]
: selection;
if (!selection) {
selection = latestRefmtString;
}
ReactDOM.render(
<Popup initialSelectedText={selection}/>,
document.getElementById('app'),
);
});
});
3 changes: 2 additions & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
}
},
"permissions": [
"activeTab"
"activeTab",
"storage"
],
"web_accessible_resources": [
"ocamlDoc.css",
Expand Down

0 comments on commit 6ce3714

Please sign in to comment.