extract-links.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /* Extract links from a page
  2. */
  3. (function () {
  4. /* --- copy&paste from click.js --- */
  5. /* Element is visible if itself and all of its parents are
  6. */
  7. function isVisible (o) {
  8. if (o === null || !(o instanceof Element)) {
  9. return true;
  10. }
  11. let style = window.getComputedStyle (o);
  12. if ('parentNode' in o) {
  13. return style.display !== 'none' && isVisible (o.parentNode);
  14. } else {
  15. return style.display !== 'none';
  16. }
  17. }
  18. /* Elements are considered clickable if they are a) visible and b) not
  19. * disabled
  20. */
  21. function isClickable (o) {
  22. return !o.hasAttribute ('disabled') && isVisible (o);
  23. }
  24. /* --- end copy&paste */
  25. let ret = [];
  26. ['a[href]', 'area[href]'].forEach (function (s) {
  27. let x = document.querySelectorAll(s);
  28. for (let i=0; i < x.length; i++) {
  29. if (isClickable (x[i])) {
  30. ret.push (x[i].href);
  31. }
  32. }
  33. });
  34. /* If Chrome loads plain-text documents it’ll wrap them into <pre>. Check those
  35. * for links as well, assuming the whole line is a link (i.e. list of links). */
  36. let x = document.querySelectorAll ('body > pre');
  37. for (let i=0; i < x.length; i++) {
  38. if (isVisible (x[i])) {
  39. x[i].innerText.split ('\n').forEach (function (s) {
  40. if (s.match ('^https?://')) {
  41. ret.push (s);
  42. }
  43. });
  44. }
  45. }
  46. return ret; /* immediately return results, for use with Runtime.evaluate() */
  47. })();