JavaScript Add content to the beginning of every line, like ::before does for the first line

I am trying to style my <code>/<pre> tags without preventing the user from being able to highlight and copy the code properly.

The way I was hoping to do this (see below) only works for the first line, and won't ever repeat.

I'm sure this could be made to work using JavaScript, but I'm not sure what the best way to do that without a large amount of processing would be.

body {
  font-family: sans-serif;
}

code, pre {
  background: #e5f3ff;
}

code.styled,
pre.styled {
  display: block;
  padding: 8px;
  margin: 8px 0;
  overflow-x: auto;
}

code.styled::before,
pre.styled::before {
  content: "–";
  padding-right: 8px;
}
<p>This is an example of using <code>::before</code> to add content,<br>
and still being able to highlight/copy text without copying prefix.</p>
<pre class="styled">
adb wait-for-device
adb reboot-bootloader
fastboot devices
</pre>
<p>Note: You can copy the code without having to worry about the prefix.</p>
Answer:1

Here is a pure javascript solution

var _pre = document.querySelector("pre.styled");
_pre.innerHTML="<span class='line'>"+(_pre.textContent.split("\n").filter(Boolean).join("</span>\n<span class='line'>"))+"</span>";
body {
  font-family: sans-serif;
}

code, pre {
  background: #e5f3ff;
}

code.styled,
pre.styled {
  display: block;
  padding: 8px;
  margin: 8px 0;
  overflow-x: auto;
}

code.styled .line::before,
pre.styled .line::before {
  content: "–";
  padding-right: 8px;
}
<p>This is an example of using <code>::before</code> to add content,<br>
and still being able to highlight/copy text without copying prefix.</p>
<pre class="styled">
adb wait-for-device
adb reboot-bootloader
fastboot devices
</pre>
<p>Note: You can copy the code without having to worry about the prefix.</p>
Answer:2

If you don't want to wrap each line in it's own tag, you could try a background image technique similar to this answer:

styling each line inside pre with css

http://www.dte.web.id/2012/03/css-only-zebra-striped-pre-tag.html#.UUoV6lugkoM

UPDATE: added code sample

body {
  font-family: sans-serif;
}

code, pre {
  background: #e5f3ff;
}

code.styled,
pre.styled {
  display:block;
  font:normal 12px/22px Monaco,Monospace !important;
  color:#000;
  background-color:#e5f3ff;
  background-image:radial-gradient(circle at 50%, #333 0%, #333 10%, #e5f3ff 20%);
  background-size: 22px 22px;
  background-repeat: repeat-y;
  padding:0em 20px;
  overflow:auto;
}
<pre class="styled">
adb wait-for-device
adb reboot-bootloader
fastboot devices
</pre>
<p>Note: You can copy the code without having to worry about the prefix.</p>
Answer:3

How about just use a bit of javascript to wrap each line with spans and style them:

$('pre.newline').each( function() {
  var text = $(this).text().split('\n');
  $(this).html('')

  for(var i = 0; i <  text.length; i++) {
    $(this).append( $('<span>').html( text[i] ) );
  }

  $(this).html(html)
})

And style the spans display:block

pre.newline span::before {
  content: "–";
  padding-right: 8px;
}

pre.newline span {
  display: block;
}
Answer:4

I am doing a request in this way using AngularJS: var config = { headers: { 'Authorization': 'somehash', 'Content-Type': 'application/json; charset=UTF-8' } }; $...

I am doing a request in this way using AngularJS: var config = { headers: { 'Authorization': 'somehash', 'Content-Type': 'application/json; charset=UTF-8' } }; $...

  1. angularjs http request default timeout
  2. angularjs http request timeout
  3. angularjs http request progress bar
  4. angularjs http request in loop
  5. angularjs http request interceptor example
  6. angularjs http request
  7. angularjs http request interceptor
  8. angularjs http request error handling
  9. angularjs http request synchronous
  10. angularjs http request parameters
  11. angularjs http request cors
  12. angularjs http request body
  13. angularjs http request json
  14. angularjs http request multipart/form-data
  15. angularjs http request cross domain
  16. angularjs http request method options
  17. angularjs http request content type
  18. angularjs http request form data
  19. angularjs http request async
  20. angularjs http request cookie

I've got an array var itemsArr = ["blue", "wine", "orange", "yellow", "cyan"]; How can I get a certain amount of array elements from the END to the FRONT of the array? Expected output: example 1: ...

I've got an array var itemsArr = ["blue", "wine", "orange", "yellow", "cyan"]; How can I get a certain amount of array elements from the END to the FRONT of the array? Expected output: example 1: ...

I received Binary Data as String, and how do I convert it to JS Number with little endian CPU? Does ArrayBuffer, DataView useful? It is not just radix converting question. 16 in 2 radix is 10000 not ...

I received Binary Data as String, and how do I convert it to JS Number with little endian CPU? Does ArrayBuffer, DataView useful? It is not just radix converting question. 16 in 2 radix is 10000 not ...

  1. binary number data type
  2. binary number data
  3. data binary number system
  4. binary number data in c
  5. binary number data type sql
  6. binary data to number python
  7. c binary number data type

I make an AJAX call to my server to read a file and get the value back as response. If the request was successful then i replace the innerHTML from my <p> element with id output with the ...

I make an AJAX call to my server to read a file and get the value back as response. If the request was successful then i replace the innerHTML from my <p> element with id output with the ...

  1. detect variable change javascript
  2. detect variable change angular 6
  3. detect variable change angular
  4. detect variable change c#
  5. detect variable change js
  6. detect variable change angular 5
  7. detect variable change python
  8. detect variable change jquery
  9. detect variable change
  10. detect variable change angular 7
  11. detect variable change angular 2
  12. detect variable change angular 8
  13. detect variable change matlab
  14. java detect variable change
  15. ngonchanges detect variable change
  16. unity detect variable change
  17. angular detect variable change in component
  18. angular detect variable change in service
  19. angularjs detect variable change
  20. vue detect variable change