Skip to content

Commit

Permalink
Use page title when getSelection fails
Browse files Browse the repository at this point in the history
  • Loading branch information
hnakamur committed Jun 24, 2017
1 parent 8d50441 commit d494ccf
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 27 deletions.
85 changes: 62 additions & 23 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,76 @@ gettingOptions().then(options => {
var url = info.linkUrl ? info.linkUrl : info.pageUrl;
var title = tab.title;
var text = info.selectionText;
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText).
then(() => {
var ctrlPressed = info.modifiers.includes('Ctrl');
if (ctrlPressed) {
saveDefaultFormat(formatID);
try {
if (text) {
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText).
then(() => {
var ctrlPressed = info.modifiers.includes('Ctrl');
if (ctrlPressed) {
saveDefaultFormat(formatID);
}
});
} else {
browser.tabs.sendMessage(tab.id, {"method": "getSelection"}).
then(response => {
text = response.selection;
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText).
then(() => {
var ctrlPressed = info.modifiers.includes('Ctrl');
if (ctrlPressed) {
saveDefaultFormat(formatID);
}
});
}).catch(reason => {
var formattedText = formatURL(format, url, title);
copyTextToClipboard(formattedText).
then(() => {
var ctrlPressed = info.modifiers.includes('Ctrl');
if (ctrlPressed) {
saveDefaultFormat(formatID);
}
});
});
}
});
} catch (e) {
console.error("FormatLink extension failed to copy URL to clipboard.");
}
});
}
});
});

browser.commands.onCommand.addListener((command) => {
if (command === 'format-link-in-default-format') {
browser.tabs.query({active: true, currentWindow: true}).then(tabs => {
if (tabs[0]) {
var tab = tabs[0];
browser.tabs.sendMessage(tab.id, {"method": "getSelection"}).
then(response => {
gettingOptions().then(options => {
var defaultFormatID = options['defaultFormat'];
var format = options['format' + defaultFormatID];
var url = tab.url;
var title = tab.title;
var text = response.selection;
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText);
});
});
}
gettingOptions().then(options => {
browser.tabs.query({active: true, currentWindow: true}).then(tabs => {
if (tabs[0]) {
var tab = tabs[0];
try{
browser.tabs.sendMessage(tab.id, {"method": "getSelection"}).
then(response => {
var defaultFormatID = options['defaultFormat'];
var format = options['format' + defaultFormatID];
var url = tab.url;
var title = tab.title;
var text = response.selection;
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText);
}).catch(reason => {
var defaultFormatID = options['defaultFormat'];
var format = options['format' + defaultFormatID];
var url = tab.url;
var title = tab.title;
var formattedText = formatURL(format, url, title);
copyTextToClipboard(formattedText);
});
} catch (e) {
console.error("FormatLink extension failed to copy URL to clipboard.");
}
}
});
});
}
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Format Link",
"version": "1.3.0",
"version": "1.3.1",
"manifest_version": 2,
"description": "Format a link and copy it to the clipboard.",
"icons": {
Expand Down
8 changes: 5 additions & 3 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ function init() {
browser.tabs.query({active: true, currentWindow: true}).then(tabs => {
if (tabs[0]) {
var tab = tabs[0];
browser.tabs.sendMessage(tab.id, {"method": "getSelection"}).
then(response => {
gettingOptions().then(options => {
gettingOptions().then(options => {
browser.tabs.sendMessage(tab.id, {"method": "getSelection"}).
then(response => {
populateFields(options, tab.url, tab.title, response.selection);
}).catch(reason => {
populateFields(options, tab.url, tab.title);
});
});
}
Expand Down

0 comments on commit d494ccf

Please sign in to comment.