Skip to content

Commit

Permalink
Add shortcut Alt+C to copy the URL in the default format
Browse files Browse the repository at this point in the history
  • Loading branch information
hnakamur committed Jun 18, 2017
1 parent 780c992 commit 8d50441
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 32 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Open the context menu "Format Link", and select one of sub menus for the format

If you select a sub menu with a Control key pressed, then the selected format is becoming the default format.

### keyboard shortcut
Press the shortcut key Alt+C to copy the URL in the default format.
If you selected some text, the selected text is used instead of the page title.

## Flexible settings
You can modify formats in [Tools] -> [Extensions] -> Clik "Options" link in "Format Link" Extension.
In format settings, you can use the mini template language.
Expand All @@ -27,7 +31,8 @@ In format settings, you can use the mini template language.
* The value of the variable `url` is the link URL if selection contains a link AND
you initiate a copy with a context menu.
Otherwise, the value of variable `url` is the HTML page URL.
* Note it is always the HTML page URL if you use the toolbar button to copy.
* Note it is always the HTML page URL if you use the toolbar button or the
keyboard shortcut to copy.
This behavior will be changed if I find a way to get a link URL in selection
when no context menu is selected.
* No spaces are allowed between variable name and braces.
Expand Down
86 changes: 56 additions & 30 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
function copyTextToClipboard(text) {
// The example will show how data can be copied, but since background
// pages cannot directly write to the clipboard, we will run a content
// script that copies the actual content.

// clipboard-helper.js defines function copyToClipboard.
const code = "copyToClipboard(" + JSON.stringify(text) + ");";

browser.tabs.executeScript({
code: "typeof copyToClipboard === 'function';",
}).then(results => {
// The content script's last expression will be true if the function
// has been defined. If this is not the case, then we need to run
// clipboard-helper.js to define function copyToClipboard.
if (!results || results[0] !== true) {
return browser.tabs.executeScript({
file: "clipboard-helper.js",
});
}
}).then(() => {
return browser.tabs.executeScript({
code,
});
}).catch(error => {
// This could happen if the extension is not allowed to run code in
// the page, for example if the tab is a privileged page.
console.error("Failed to copy text: " + error);
});
}

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

Expand All @@ -11,42 +41,38 @@ gettingOptions().then(options => {
var format = options['format' + formatID];
var url = info.linkUrl ? info.linkUrl : info.pageUrl;
var title = tab.title;
var text = info.selectionText ? info.selectionText : tab.title;
var formattedText = formatURL(format , url, title, text);

// The example will show how data can be copied, but since background
// pages cannot directly write to the clipboard, we will run a content
// script that copies the actual content.

// clipboard-helper.js defines function copyToClipboard.
const code = "copyToClipboard(" + JSON.stringify(formattedText) + ");";

browser.tabs.executeScript(tab.id, {
code: "typeof copyToClipboard === 'function';",
}).then(results => {
// The content script's last expression will be true if the function
// has been defined. If this is not the case, then we need to run
// clipboard-helper.js to define function copyToClipboard.
if (!results || results[0] !== true) {
return browser.tabs.executeScript(tab.id, {
file: "clipboard-helper.js",
});
}
}).then(() => {
return browser.tabs.executeScript(tab.id, {
code,
});
}).then(() => {
var text = info.selectionText;
var formattedText = formatURL(format, url, title, text);
copyTextToClipboard(formattedText).
then(() => {
var ctrlPressed = info.modifiers.includes('Ctrl');
if (ctrlPressed) {
saveDefaultFormat(formatID);
}
}).catch(error => {
// This could happen if the extension is not allowed to run code in
// the page, for example if the tab is a privileged page.
console.error("Failed to copy text: " + error);
});
});
}
});
});

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);
});
});
}
});
}
});
10 changes: 9 additions & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Format Link",
"version": "1.2.0",
"version": "1.3.0",
"manifest_version": 2,
"description": "Format a link and copy it to the clipboard.",
"icons": {
Expand All @@ -25,6 +25,14 @@
"default_popup": "popup.html",
"browser_style": true
},
"commands": {
"format-link-in-default-format": {
"suggested_key": {
"default": "Alt+C"
},
"description": "Copy the link to the clipboard in default format"
}
},
"content_scripts": [
{
"matches": ["<all_urls>"],
Expand Down

0 comments on commit 8d50441

Please sign in to comment.