How To: One-click copy selected text to [article] format

Mick West

Administrator
Staff member
The [article] format is a useful way of quoting a snippet from web page, and looks like this.

Article:
FULTON COUNTY, Ga. -- According to Fox 5, Fulton County has a delay with processing and counting tens of thousands of absentee ballots due to a pipe break in the State Farm Arena.


To do this by hand you use code like:
[article=https://wgxa.tv/news/beyond-the-podium/pipe-burst-at-state-farm-arena-causes-delay-in-absentee-vote-counts-in-fulton-county]FULTON COUNTY, Ga. -- According to Fox 5, Fulton County has a delay with processing and counting tens of thousands of absentee ballots due to a pipe break in the State Farm Arena.[/article]

However an easier way is to add a bookmarklet to your browser, with the code here: (also in the attached zip file.)

JavaScript:
javascript:(function() {

function copyToClipboard(text) {
    if (window.clipboardData && window.clipboardData.setData) {
        /*IE specific code path to prevent textarea being shown while dialog is visible.*/
        return clipboardData.setData("Text", text);

    } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
        var textarea = document.createElement("textarea");
        textarea.textContent = text;
        textarea.style.position = "fixed";  /* Prevent scrolling to bottom of page in MS Edge.*/
        document.body.appendChild(textarea);
        textarea.select();
        try {
            return document.execCommand("copy");  /* Security exception may be thrown by some browsers.*/
        } catch (ex) {
            console.warn("Copy to clipboard failed.", ex);
            return false;
        } finally {
            document.body.removeChild(textarea);
        }
    }
}

var markdown = '[article=' + window.location.href + ']'+window.getSelection().toString()+'[/article]';
copyToClipboard(markdown);
})();

The simplest way to do this is to create a bookmark, then edit it, rename it to something like [article] and then copy in the code.

Then just highlight the text you want, and click the bookmarklet, and then paste into a post. Magic!
 

Attachments

  • article-bookmarklet.txt.zip
    1 KB · Views: 415
Last edited:
I can't find a button for it on the toolbar; that's why I type the code manually.
I like it because it lets me neatly attribute my quotes with a link to the source.
 
where in the code can i change the "article" heading to "external source"? or can i not do that? (sometimes it's not really an article being quoted)
 
where in the code can i change the "article" heading to "external source"? or can i not do that? (sometimes it's not really an article being quoted)
All of the code that makes the link is in the one line:

Code:
var markdown = '[article=' + window.location.href + ']'+window.getSelection().toString()+'[/article]';

If you make a version replacing that line with

Code:
var markdown = window.location.href + '\n[ex]'+window.getSelection().toString()+'[/ex]';

It will give you it in ex form, with the URL above it.

The \n is a newline.
 
Back
Top