0

For a Firefox (or Chrome) addon, I want a content script to send messages containing text IDs and urls to a background script. I want the background script to output the IDs and urls as simple strings using console.log(), but the urls are coming out with quotes and hyperlinks.

How can I get the urls to be output as simple strings?

Here's, for example, what I am aiming for console.log() to output:

The url is: h!!ps://upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg

Content script:

// content-script.js

var identifier = "Sirius";
var link = "https://upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg";
var strippedLink = "upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg";

var myPort = browser.runtime.connect({name:"port-from-cs"});
myPort.postMessage({theId: identifier, theUrl: link, theStripped: strippedLink});

Background script:

// background-script.js

var portFromCS;

function connected(p) {
  portFromCS = p;
  portFromCS.onMessage.addListener(function(m) {
    var ident = m.theId; 
    var url = m.theUrl;
    var altUrl = m.theStripped;

    // (A)
    console.log("(A) The ID is: " + ident);

    // (B)
    console.log("(B) The stripped url is: " + altUrl);

    // (C)
    console.log("(C) The url is: " + url);

    // (D)
    console.log("(D) The url is: ");
    console.log(url);
  });
};


browser.runtime.onConnect.addListener(connected);

(A) outputs the format I am aiming for (except A doesn't have any urls).

(B) is also ok, except that I would want to be able to include the protocol (http or https) in the original string that is sent in the message.

(C), and (D) result in hyperlinks and quotes.

edit:

What I want is

The url is: https://upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg

copy/paste from console follows (references to code lines removed):

(A) The ID is: Sirius

(B) The stripped url is: upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg

"(C) The url is: https://upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg"

(D) The url is:
"https://upload.wikimedia.org/wikipedia/commons/c/c6/Sirius.jpg"

Cam U
  • 340
  • 3
  • 10
  • What, *exactly*, was shown in the [Browser Console](https://developer.mozilla.org/en-US/docs/Tools/Browser_Console) (Ctrl-Shift-J, or Cmd-Shift-J on OSX) when you tried to install and use the extension? Please show the exact output you now get and show what you actually want to achieve. – Makyen Dec 21 '16 at 21:30
  • @Makyen, I don't yet have enough reputation points to be able to post images. I have a screencap at imgur with code fwwIGST that shows the ouput currently being generated by the code I posted already. I want the format to be just like the output for (B), except it needs to include the https:// with the rest of the link text, but no hyperlink and no quotation marks. – Cam U Dec 22 '16 at 01:35
  • (Aside from that, is there a way to suppress automatic hyperlinking of urls in stackoverflow question posts?) – Cam U Dec 22 '16 at 01:36
  • Anything in code format will not be automatically interpreted as something else. Code format: short: enclose in back-ticks `\``; long: precede by blank line and indent by 4 spaces. Your image location redirected to a statement that it was removed. While an image may be appropriate, depending on what you are trying to show, it is usually *much* better to copy and paste the contents of the console into the question as text and put the information in code format (usually indented by 4 spaces). There should be a `?` in the upper right corner of the edit window linked to markdown syntax help. – Makyen Dec 22 '16 at 02:21
  • I edited the question to add the output copied from Firefox console.log(). – Cam U Dec 22 '16 at 19:54

0 Answers0