Skip to content

Commit

Permalink
Merge pull request #6 from hnakamur/use_query_selector_to_get_link_text
Browse files Browse the repository at this point in the history
Use query selector to get link text
  • Loading branch information
hnakamur authored Aug 23, 2017
2 parents d494ccf + 37e6964 commit f5054f8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ Here are examples:
"{{title.s("\"",""").s("\\[","[")}}":{{url}}
```

## KNOWN LIMITATIONS

* Due to web extension API limitations, this extension does not work as users might expect.
* You cannot use the toolbar nor the keyboard shortcut if you want to copy a link URL. With those two, the URL always becomes the page URL.
* Due to security reason, the limitation becomes severer on about: pages and addons.mozilla.org pages.
* You cannot use the context menus nor the keyboard shortcut on those pages.
* You can use the toolbar to copy the URL, but the text always become the page title even if you select some text.

## License
MIT License.
Source codes are hosted at [Github](https://github.com/hnakamur/FormatLink-Firefox)
Expand Down
48 changes: 24 additions & 24 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ function copyTextToClipboard(text) {
});
}

function tryToGetLinkSelectionText(tab, url, text) {
if (text) {
return Promise.resolve(text);
}
return browser.tabs.sendMessage(tab.id, {"method": "getSelection"}).
then(response => {
var selText = response.selection;
if (selText) {
return selText;
}
return browser.tabs.executeScript(tab.id, {
code: `
var link = document.querySelector('a[href="${url}"]');
link ? link.innerText.trim() : "";
`
}).then(response => {
return response[0];
});
});
}

gettingOptions().then(options => {
createContextMenus(options);

Expand All @@ -43,7 +64,8 @@ gettingOptions().then(options => {
var title = tab.title;
var text = info.selectionText;
try {
if (text) {
tryToGetLinkSelectionText(tab, url, text).
then(text => {
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText).
then(() => {
Expand All @@ -52,29 +74,7 @@ gettingOptions().then(options => {
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.");
}
Expand Down

0 comments on commit f5054f8

Please sign in to comment.