Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use query selector to get link text #6

Merged
merged 2 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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