util.lua 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. --公共文件
  2. --获取table长度
  3. function table_length(t)
  4. if type(t)~="table" then
  5. return 0
  6. end
  7. local len=0
  8. for _,_ in pairs(t) do
  9. len=len+1
  10. end
  11. return len
  12. end
  13. --保存网页为pdf文件
  14. function save_page_to_pdf(tab_title,tab_href,dir,title)
  15. local filename = ""
  16. if string.find(title,"2024年")~=nil then
  17. filename = "2024_"..title.."__浏览器生成.pdf"
  18. elseif string.find(title,"2023年")~=nil then
  19. filename = "2023_"..title.."__浏览器生成.pdf"
  20. else
  21. filename = "2022_"..title.."__浏览器生成.pdf"
  22. end
  23. print("准备生成网页pdf",filename)
  24. local ok2 = browser_print2pdf(tab_href,1000*60*20,dir..filename)
  25. print("生成pdf",ok2)
  26. end
  27. --页面清理元素,只保留中心元素
  28. local remote_body_el_without_js=[[
  29. var hold_el = document.querySelector("%s");
  30. function removeAllChildren(parent) {
  31. var children = parent.childNodes;
  32. for (var i = children.length - 1; i >= 0; i--) {
  33. parent.removeChild(children[i]);
  34. }
  35. }
  36. removeAllChildren(document.body);
  37. document.body.appendChild(hold_el);
  38. ]]
  39. --页面清理元素,只保留中心元素
  40. function clear_page_el(tab_title,tab_href,timeout,css_selector)
  41. local js = string.format(remote_body_el_without_js, css_selector)
  42. local ok, ret = browser_executejs(tab_title,tab_href,timeout,0, js)
  43. return ok,ret
  44. end