Javascript 以编程方式将w3c css/html验证程序与自定义html一起使用 编辑

Javascript 以编程方式将w3c css/html验证程序与自定义html一起使用 编辑,javascript,validation,web-crawler,Javascript,Validation,Web Crawler,我已经让它为CSS工作,只有HTML验证 let css = Array.from(document.styleSheets) .reduce((combinedsheet,sheet) => sheet.rules? combinedsheet + Array.from(sheet.rules) .reduce((p,c) => p+c.cssText+'\n', ''): combinedsheet

我已经让它为CSS工作,只有HTML验证

let css = Array.from(document.styleSheets)
        .reduce((combinedsheet,sheet) => sheet.rules? 
          combinedsheet + Array.from(sheet.rules)
            .reduce((p,c) => p+c.cssText+'\n', ''):
          combinedsheet, '')        
      try {
        document.querySelector('a.validation.css').href = 
          'https://jigsaw.w3.org/css-validator/validator?text=' +
          encodeURIComponent(css) +
          '&profile=css3&usermedium=all&warning=1&vextwarning='

        document.querySelector('a.validation.html').href = 
          'https://validator.w3.org/nu/?doc=' +
          encodeURIComponent(document.querySelector('html'))
      } catch (e) {
        // this will fail before page fully loads, and we can be silent about that
      }
编辑#2 我让它工作了。唯一的问题是,它使用“弹出窗口”,而不是像
target=“blank”
那样以静默方式打开窗口。我使用的是onlick方法:

get_html_validation: function () {
    let fd = new FormData()
    fd.append('fragment', '<!DOCTYPE html>' + 
      document.querySelector('html').outerHTML)
    fd.append('prefill', 0)
    fd.append('doctype', 'Inline')
    fd.append('prefill_doctype', 'html401')
    fd.append('group', 0)

    axios.post("https://validator.w3.org/nu/#textarea", fd)
      .then(response => {
        let win=window.open('about:blank')
        console.log(win)
        with(win.document)
        {
          open()
          write(response.data)
          close()
        }
      })
      .catch(e => console.error(e))
  }   
get\u html\u验证:函数(){
设fd=newformdata()
fd.append('fragment',''+
document.querySelector('html').outerHTML)
fd.append('prefill',0)
fd.append('doctype','Inline')
fd.append('prefill\u doctype','html401')
fd.append('group',0)
axios.post(“https://validator.w3.org/nu/#textarea“,fd)
。然后(响应=>{
let win=window.open('about:blank')
console.log(win)
使用(win.document)
{
开()
写入(响应.数据)
关闭()
}
})
.catch(e=>console.error(e))
}   
起初的 我希望以编程方式使用这些验证器:

-如果将URL作为参数传递,只要文档没有操纵dom的javascript,它们就可以正常工作。但是我的是

如何在浏览器中使用javascript将自定义html交给这些站点进行检查?我希望做一些类似的事情:

onValidateDOM = () => {
  let toValidate = '<!DOCTYPE html>' + document.querySelector('html').outerHTML
  let returnURLs = []
  returnURLs.push(w3cCSS.validate(toValidate), w3cHTML.validate(toValidate)
  return returnURLs
}
onValidateDOM=()=>{
让toValidate=''+document.querySelector('html').outerHTML
让returnURL=[]
returnURLs.push(w3cCSS.validate(toValidate),w3cHTML.validate(toValidate)
返回URL
}

设置为验证
传递给
encodeURIComponent()
作为
doc=
查询的值

fetch(`https://validator.w3.org/nu/?doc=${encodeURIComponent(toValidate)}`)
.then(response => response.text())
.then(text => console.log(text));

@帕特里克巴尔:你是想把那条评论留在别的地方吗?不,对不起,我在编辑之前误解了你的问题,很高兴我当时编辑了它:)把数据以他们的“检查方式”
文本输入
工作的方式发布给他们怎么样?看起来有一个和一个,嗯,不幸的是,不是。参数
doc
接受URL,而不是来自file@robertotomás显然没有提供
访问控制允许来源
标题。尽管在域
var fd=new FormData()中可以执行以下操作:;追加(“文本”,“正文{color:red;}”);取回(“https://jigsaw.w3.org/css-validator/validator“,{method:“POST”,body:fd})。然后(response=>response.text())。然后(text=>console.log(text))
,结果应该是。您可以尝试使用
YQL
发出请求,我注意到我可以使用text=并为CSS验证器传入URL编码的CSS。就这样一个倒下了!编辑:将实际代码添加到post)