Copy selected (highlighted) link text & plain t

General discussion about Custom Buttons, including feature requests.

Copy selected (highlighted) link text & plain t

Postby seamoon » Thu Jul 13, 2017 8:30 pm

I have the button below. Just want to copy selected (highlighted) link text & plain text together to clipboard. How can I do this?

Example:
Go to site
http://www.imdb.com/title/tt0066492/?ref_=adv_li_tt
Highlight "Trog (1970)" (which is combination of text + link) and send to clipboard.

/*Code*/
goDoCommand("cmd_copy");
var keyword = "y";
var selection = getBrowserSelection();
getShortcutOrURIAndPostData(keyword + " " + selection, function (data) {
openUILinkIn(data.url, "tab", false, null, null);
});
seamoon
 
Posts: 36
Joined: Thu Oct 09, 2014 5:39 pm

Re: Copy selected (highlighted) link text & plain t

Postby morat » Thu Jul 13, 2017 9:59 pm

Try this:

Code: Select all
/*Code*/
var out = [];
var win = document.commandDispatcher.focusedWindow;
if (win == window) {
  win = window.content;
}
var sel = win.getSelection();
if (!sel.isCollapsed) {
  var doc = win.document;
  out.push(win.getSelection().toString());
  for (var i = 0; i < sel.rangeCount; i++) {
    var range = sel.getRangeAt(i);
    var fragment = range.cloneContents();
    var treeWalker = fragment.ownerDocument.createTreeWalker(fragment,
      NodeFilter.SHOW_ELEMENT, null, true);
    while (treeWalker.nextNode()) {
      var node = treeWalker.currentNode;
      if ((node instanceof HTMLAnchorElement ||
           node instanceof HTMLAreaElement) && node.href) {
        try {
          urlSecurityCheck(node.href, doc.nodePrincipal,
            Components.interfaces.nsIScriptSecurityManager.DISALLOW_SCRIPT);
          out.push(unescape(node.href));
        } catch (e) {}
      }
    }
  }
}
var isWindows = navigator.platform.indexOf("Win") != -1;
var platformDelimiter = isWindows ? "\r\n" : "\n";
custombuttons.cbService.writeToClipboard(out.join(platformDelimiter));

I got the TreeWalker code from the following button.

Copy URL
viewtopic.php?f=4&t=263
morat
 
Posts: 959
Joined: Wed Dec 28, 2011 2:38 pm

Re: Copy selected (highlighted) link text & plain t

Postby seamoon » Fri Jul 14, 2017 8:50 am

But this code lack of my code (Search selected text in a search engine specified by "y" ) BTW is it possible to do this via easy way maybe triggering ctrl+c

Code: Select all
goDoCommand("cmd_copy");
var keyword = "y";
var selection = getBrowserSelection();
getShortcutOrURIAndPostData(keyword + " " + selection, function (data) {
openUILinkIn(data.url, "tab", false, null, null);
});
seamoon
 
Posts: 36
Joined: Thu Oct 09, 2014 5:39 pm

Re: Copy selected (highlighted) link text & plain t

Postby morat » Fri Jul 14, 2017 11:16 am

Sorry, I don't understand the question.

If I highlight "Trog (1970)" on the test page and left click the button, then the clipboard contains the selected text and url.

Trog (1970)
http://www.imdb.com/year/1970/?ref_=tt_ov_inf

Test page
http://www.imdb.com/title/tt0066492/

For example, let's say I'm using a yahoo bookmark with "y" as my keyword.

Bookmark Name: Yahoo
Bookmark Location: https://search.yahoo.com/search?q=%s
Bookmark Keyword: y

Do you want to somehow combine the TreeWalker and keyword search code snippets together?

Code: Select all
/*Code*/

// TreeWalker code snippet
var out = [];
var win = document.commandDispatcher.focusedWindow;
if (win == window) {
  win = window.content;
}
var sel = win.getSelection();
if (!sel.isCollapsed) {
  var doc = win.document;
  out.push(win.getSelection().toString());
  for (var i = 0; i < sel.rangeCount; i++) {
    var range = sel.getRangeAt(i);
    var fragment = range.cloneContents();
    var treeWalker = fragment.ownerDocument.createTreeWalker(fragment,
      NodeFilter.SHOW_ELEMENT, null, true);
    while (treeWalker.nextNode()) {
      var node = treeWalker.currentNode;
      if ((node instanceof HTMLAnchorElement ||
           node instanceof HTMLAreaElement) && node.href) {
        try {
          urlSecurityCheck(node.href, doc.nodePrincipal,
            Components.interfaces.nsIScriptSecurityManager.DISALLOW_SCRIPT);
          out.push(unescape(node.href));
        } catch (e) {}
      }
    }
  }
}
var isWindows = navigator.platform.indexOf("Win") != -1;
var platformDelimiter = isWindows ? "\r\n" : "\n";
custombuttons.cbService.writeToClipboard(out.join(platformDelimiter));

// keyword search code snippet
var keyword = "y";
var selection = getBrowserSelection();
getShortcutOrURIAndPostData(keyword + " " + selection, function (data) {
  openUILinkIn(data.url, "tab", false, data.postData, null);
});
morat
 
Posts: 959
Joined: Wed Dec 28, 2011 2:38 pm

Re: Copy selected (highlighted) link text & plain t

Postby srazzano » Fri Jul 14, 2017 12:11 pm

NM
Last edited by srazzano on Fri Jul 14, 2017 11:18 pm, edited 1 time in total.
srazzano
 
Posts: 1545
Joined: Sat Mar 31, 2012 6:42 pm
Location: Las Vegas, Nevada

Re: Copy selected (highlighted) link text & plain t

Postby seamoon » Fri Jul 14, 2017 5:15 pm

morat wrote:Sorry, I don't understand the question.

If I highlight "Trog (1970)" on the test page and left click the button, then the clipboard contains the selected text and url.


Test page
http://www.imdb.com/title/tt0066492/

For example, let's say I'm using a yahoo bookmark with "y" as my keyword.

Bookmark Name: Yahoo
Bookmark Location: https://search.yahoo.com/search?q=%s
Bookmark Keyword: y

Do you want to somehow combine the TreeWalker and keyword search code snippets together?

Code: Select all
/*Code*/

// TreeWalker code snippet
var out = [];
var win = document.commandDispatcher.focusedWindow;
if (win == window) {
  win = window.content;
}
var sel = win.getSelection();
if (!sel.isCollapsed) {
  var doc = win.document;
  out.push(win.getSelection().toString());
  for (var i = 0; i < sel.rangeCount; i++) {
    var range = sel.getRangeAt(i);
    var fragment = range.cloneContents();
    var treeWalker = fragment.ownerDocument.createTreeWalker(fragment,
      NodeFilter.SHOW_ELEMENT, null, true);
    while (treeWalker.nextNode()) {
      var node = treeWalker.currentNode;
      if ((node instanceof HTMLAnchorElement ||
           node instanceof HTMLAreaElement) && node.href) {
        try {
          urlSecurityCheck(node.href, doc.nodePrincipal,
            Components.interfaces.nsIScriptSecurityManager.DISALLOW_SCRIPT);
          out.push(unescape(node.href));
        } catch (e) {}
      }
    }
  }
}
var isWindows = navigator.platform.indexOf("Win") != -1;
var platformDelimiter = isWindows ? "\r\n" : "\n";
custombuttons.cbService.writeToClipboard(out.join(platformDelimiter));

// keyword search code snippet
var keyword = "y";
var selection = getBrowserSelection();
getShortcutOrURIAndPostData(keyword + " " + selection, function (data) {
  openUILinkIn(data.url, "tab", false, data.postData, null);
});


Actually I want only text not link (I mentioned before link text for this) I tried these commands:
var args = ["sendkeypress", "ctrl+0x43"];
and
goDoCommand("cmd_copy");
but for plain text + link text (not link address) and couldn't succeed.
Your code works but could you remove link copying function?
seamoon
 
Posts: 36
Joined: Thu Oct 09, 2014 5:39 pm

Re: Copy selected (highlighted) link text & plain t

Postby srazzano » Fri Jul 14, 2017 9:59 pm

NM
Last edited by srazzano on Fri Jul 14, 2017 11:17 pm, edited 1 time in total.
srazzano
 
Posts: 1545
Joined: Sat Mar 31, 2012 6:42 pm
Location: Las Vegas, Nevada

Re: Copy selected (highlighted) link text & plain t

Postby seamoon » Fri Jul 14, 2017 10:28 pm

srazzano wrote:This will copy selected/highlighted text only, no link address, to clipboard. Do you desire to open selected text in a new tab, with a search engine as well? If so, current browser's search engine, a specific search engine or a drop down menu list of specified search engines? Specifying search engine(s) would be helpful.
Code: Select all
"use strict";
var win = document.commandDispatcher.focusedWindow;
if (win == window) { win = window.content }
var sel = win.getSelection();
custombuttons.cbService.writeToClipboard(sel);


I prefer specific site via keyword search like below but your code somehow can not copied to my clipboard manager called Ditto. But morat's code works fine I just need to remove link address from his code.

Code: Select all
/*Code*/
"use strict";
var win = document.commandDispatcher.focusedWindow;
if (win == window) { win = window.content }
var sel = win.getSelection();
custombuttons.cbService.writeToClipboard(sel);
var keyword = "y";
var selection = getBrowserSelection();
getShortcutOrURIAndPostData(keyword + " " + selection, function (data) {
  openUILinkIn(data.url, "tab", false, null, null);
});
seamoon
 
Posts: 36
Joined: Thu Oct 09, 2014 5:39 pm

Re: Copy selected (highlighted) link text & plain t

Postby srazzano » Fri Jul 14, 2017 11:16 pm

Works fine here with Ditto. If Ditto is not copying selected text to the list, it's not recognizing focused text (known bug in Ditto). Remove the Ditto app and re-install.
Go into your Tools > Options > Search and ensure keywords are set for the search engines. Set the keyword you want in the script for var keyword = "your keyword here"; or click on Add more search engines... link, add the search engine you want and set a keyword for it.
Image
Code: Select all
var keyword = "google"; // Insert your keyword here
var win = document.commandDispatcher.focusedWindow;
if (win == window) { win = window.content }
var sel = win.getSelection();
custombuttons.cbService.writeToClipboard(sel);
getShortcutOrURIAndPostData(keyword + " " + sel, function (data) {
  openUILinkIn(data.url, "tab", false, null, null);
});
srazzano
 
Posts: 1545
Joined: Sat Mar 31, 2012 6:42 pm
Location: Las Vegas, Nevada

Re: Copy selected (highlighted) link text & plain t

Postby seamoon » Sat Jul 15, 2017 9:21 am

Unfortunately both yours and morat's code don't send selected text to Ditto. I tried fresh install no help.
seamoon
 
Posts: 36
Joined: Thu Oct 09, 2014 5:39 pm

Next

Return to General Discussion

cron