/fe/前端全(无)埋点之页面操作路径可视化/ellocate/jquery.ellocate.js

https://github.com/zuopf769/notebook · JavaScript · 37 lines · 37 code · 0 blank · 0 comment · 16 complexity · 1d69021c5da6a222c978cae39d43f6bc MD5 · raw file

  1. (function($) {
  2. $.fn.ellocate = function(uniqueIds) {
  3. var el = $(this)[0];
  4. var uniqIds = uniqueIds || [];
  5. var locator = { xpath: '', css: ''};
  6. var eloc = {
  7. isUniqueId: function(id, ids) { return !!($.inArray(id, ids) == -1); },
  8. getClass: function(el) {
  9. var formatClass = '';
  10. var elementClass = $(el).attr('class');
  11. if(typeof elementClass != 'undefined' && elementClass != ''){
  12. formatClass = '.' + elementClass.split(/[\s\n]+/).join('.');
  13. }
  14. return formatClass;
  15. }
  16. };
  17. for (; el && el.nodeType == 1; el = el.parentNode) {
  18. var idx = $(el.parentNode).children(el.tagName).index(el) + 1;
  19. if(el.tagName.substring(0,1) != "/") { //IE oddity: some tagNames can begin with backslash.
  20. if(el.id != 'undefined' && el.id !='' && eloc.isUniqueId(el.id, uniqIds)) {
  21. uniqIds.push(el.id);
  22. var idPath="[@id=" + "'" + el.id + "'" + "]";
  23. locator.xpath = '/' + el.tagName.toLowerCase() + idPath + locator.xpath;
  24. locator.css = el.tagName.toLowerCase() + '#' + el.id + ' > ' + locator.css;
  25. }
  26. else {
  27. idx='[' + idx + ']';
  28. locator.xpath = '/' + el.tagName.toLowerCase() + idx + locator.xpath;
  29. locator.css = el.tagName.toLowerCase() + eloc.getClass(el) + ' > ' + locator.css;
  30. }
  31. }
  32. }
  33. locator.xpath = '/' + locator.xpath;
  34. locator.css = locator.css.substr(0, locator.css.length-3);
  35. return locator;
  36. };
  37. })(jQuery);