Advertisement
ajnieves

Prompts to create a Chrome bookmarklet to scrape h2 and h3 heading tags on a page.

Jun 27th, 2023
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Prompts to create a Chrome bookmarklet to scrape h2 and h3 heading tags on a page. ##
  2.  
  3. ## Prompt to scrape H2s:
  4. 'Create a chrome bookmarklet that scrapes the h2 headings tag and copies them into a list form on my computer clipboard.'
  5.  
  6. ## Prompt to scrape H3s:
  7. Create a chrome bookmarklet that scrapes the h2 headings tag and copies them into a list form on my computer clipboard.
  8.  
  9. ## Already created bookmarklet code:
  10.  
  11. ### H2 scrape:
  12. javascript:(function() {
  13.       var h2s = Array.from(document.getElementsByTagName('h2'));
  14.       var h2Texts = h2s.map(function(h2) {
  15.                     return h2.textContent;
  16.                   });
  17.       var textToCopy = h2Texts.join('\n');
  18.       if (window.navigator && window.navigator.clipboard) {
  19.           window.navigator.clipboard.writeText(textToCopy).then(function() {
  20.               alert('Copied to clipboard');
  21.           }, function(err) {
  22.               alert('Could not copy text: ', err);
  23.           });
  24.       } else {
  25.           var textArea = document.createElement('textarea');
  26.           textArea.value = textToCopy;
  27.           document.body.appendChild(textArea);
  28.           textArea.focus();
  29.           textArea.select();
  30.           try {
  31.               var successful = document.execCommand('copy');
  32.               var msg = successful ? 'successful' : 'unsuccessful';
  33.               alert('Copy command was ' + msg);
  34.           } catch (err) {
  35.               alert('Unable to copy, unsupported browser.');
  36.           }
  37.           document.body.removeChild(textArea);
  38.       }
  39. })();
  40.  
  41.  
  42. ### H3 scrape:
  43. javascript:(function() {
  44.     var h3s = Array.from(document.getElementsByTagName('h3'));
  45.     var h3Texts = h3s.map(function(h3) { return h3.textContent; });
  46.     var textToCopy = h3Texts.join('\n');
  47.     if (window.navigator && window.navigator.clipboard) {
  48.         window.navigator.clipboard.writeText(textToCopy).then(function() {
  49.             alert('Copied to clipboard');
  50.         }, function(err) {
  51.             alert('Could not copy text: ', err);
  52.         });
  53.     } else {
  54.         var textArea = document.createElement('textarea');
  55.         textArea.value = textToCopy;
  56.         document.body.appendChild(textArea);
  57.         textArea.focus();
  58.         textArea.select();
  59.         try {
  60.             var successful = document.execCommand('copy');
  61.             var msg = successful ? 'successful' : 'unsuccessful';
  62.             alert('Copy command was ' + msg);
  63.         } catch (err) {
  64.             alert('Unable to copy, unsupported browser.');
  65.         }
  66.         document.body.removeChild(textArea);
  67.     }
  68. })();
  69.  
  70.  
  71.  
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement