【问题标题】:Console log data return as null控制台日志数据返回为空
【发布时间】:2021-07-26 06:41:04
【问题描述】:

抱歉,有点解释。所以我已经完成了我的 chrome 扩展,并且我的本地存储中已经有一个保存数据,它是 FirstName。所以现在 getElementById 是一个假设当我点击 START 时我正在填写表单的当前页面,这是 button1 希望这些清楚的事情

我还提供了我的 index.html,如果我点击开始,它应该执行 injector.js

index.html

<!DOCTYPE html>
<html>

<head>
 <link rel="stylesheet" href="style.css">
 <style>
  html,
  body {
   height: 200px;
   width: 400px;
  }
 </style>
 
</head>

<body>
 <h1>Adidas ACO</h1>
 <h2>Select your choice</h2>
 <button><a href="./home.html" target="_blank">Go for Setup</a></button>
 
 <button id="buttonstart"><script src="injector.js"></script>
 </script>START</button>
 <!-- <br>
 <br>
 <label for="Title">Title</label>
 <input type="text" id="firstnametitle" name="title" size="50" value=""/> -->


 <!--<script scr="injector.js"></script>-->
 <!--<button onclick="fillforms(fillup)">Start</button>-->
</body>
</html>

injector.js

// Get my button and localstorage data
var button1 = document.getElementById("buttonstart");
var firstName = localStorage.getItem('First Name');
var lastName = localStorage.getItem('Last Name');
var address1 = localStorage.getItem('Address 1');
var address2 = localStorage.getItem('Address 2');
var email = localStorage.getItem('Email');
var phoneNumber = localStorage.getItem('Phone Number');

/// When button is click, it will webscape and fill up 
button1.onclick = function(){
 var firstName = localStorage.getItem('First Name');
 var field1 = document.getElementsByClassName("shippingAddress-firstName");
 fillField(field1, firstName);

 console.log(field1)
 console.log(firstName)
 
}

function fillField(field1, value){
 if(field1){
  field1.value = value;
 }
}

Picture to my console values

【问题讨论】:

  • 好像 field1 (input[id="shippingAddress-firstName"]) 不存在。
  • 我第二个@SimoneRossaini,为什么不让选择器只是#shippingAddress-firstName 而不是'input[id=..]'?
  • 真 :) @SanthosRamalingam
  • 好吧,伙计们,因为我正在做一些测试,但仍然无法正常工作

标签: javascript html web-scraping google-chrome-extension console


【解决方案1】:

在文件的开头声明变量 firstname 和 field1。你必须这样做,因为在你的代码中你只能在 button1.onclick 函数中使用这些变量,因为你已经在那里声明了它们。

【讨论】:

  • 但是他只使用onclick里面的变量,不需要全局化。
  • 所以基本上它是一个 Chrome 扩展程序,当我点击按钮 1 的“开始”时它应该填写表格。假设从页面的本地存储中获取我的数据并填写表格。 @雷诺
  • 您忘记将类属性设置为我认为的输入。还有你为什么评论了一些代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多