如何从网页获取Google扩展程序的<span>值

时间:2018-02-28 15:17:22

标签: javascript html dom

我需要不断地将这个值输入到我的google扩展程序弹出窗口,或者从网页开始接下来的10秒钟(带循环)。这将是当前标签页。

<span id="serverTime">7:55:37</span>

我现在已经走了这么远;

popup.html

        <tr>
            <td colspan="3"><a id="submit" onclick="getTime();">Time</a>
        </td>
        </tr>
        <tr>
            <td><label id="time">---</label>
        </td>
        </tr>

<script src="time.js"></script>

time.js

function getTime() {
chrome.tabs.getSelected(null, function (tab) {
    chrome.tabs.executeScript(tab.id, { code: "var serverTime = document.getElementById('serverTime')" });

}); }

的manifest.json

    {
  "manifest_version": 2,

  "name": "MyExtension",
  "description": "Google Extension",
  "version": "1.0",

  "content_scripts": [
    {
      "css": [ "popup.css" ],
      "js": [ "time.js" ],
      "matches": [ "<all_urls>" ],
      "run_at": "document_start",
      "all_frames":  true
    }
  ],

  "browser_action": {
    "default_icon": "klanlar.png",
    "default_popup": "popup.html",
      "popup": "popup.html"
  },
  "background": {
    "persistent":  false,
    "script": [ "time.js" ]
  },
  "permissions": [
    "activeTab",
    "tabs",
    "<all_urls>",
    "notifications",
    "tabs"
  ]

}

我无法让服务器时间回到分机弹出窗口,有没有机会你们都可以提供帮助

更新时间.js

function getTime() {
    chrome.tabs.getSelected(null, function (tab) {
        chrome.tabs.executeScript(tab.id, { code: "var serverTime = document.getElementById('serverTime').value.toString(); alert(serverTime);" });
        document.getElementById("time").value = serverTime;

    });
}

仍然无法获取值或弹出任何警报

2 个答案:

答案 0 :(得分:1)

我认为不是使用

var serverTime = document.getElementById('serverTime')

您应该使用这个

var serverTime = document.getElementById("serverTime").innerText

答案 1 :(得分:0)

var serverTime = document.getElementById('serverTime')只获取元素。你想得到价值

var serverTime = document.getElementById('serverTime').value