Chrome扩展名setInterval或setTimeout,因为页面加载速度太慢

时间:2017-11-25 10:53:22

标签: javascript html google-chrome google-chrome-extension

我正在创建Chrome扩展程序。以下是它的工作原理:

打开扩展程序 - >在当前标签中加载新页面 - >阅读此页面的内容 - >将此内容插入扩展程序html代码(popup.html)。

它现在如何工作:打开扩展程序(ok) - >在当前标签中加载新页面(确定) - >从此页面阅读内容(?) - >将此内容插入扩展名html代码(popup.html)(不工作)。

但是,当我关闭扩展并再次打开它时,它可以工作。 我认为这是因为页面加载太慢而且我需要在步骤3或4中设置超时或间隔。我已经尝试了setInterval和setTimeout函数,但它失败了。

这是我的popup.html(我删除了不必要的内容):

<!DOCTYPE html>
<html>
<head>
<meta name="myName" content="empty">
</head>
<body>
page content
</body>
</html>

popup.js:

// Inject the payload.js script into the current tab after the popout has loaded
window.addEventListener('load', function (evt) {
    chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
        file: 'payload.js'
    });
    chrome.tabs.update(null, {url:"https://mypage.com"});;

});

// Listen to messages from the payload.js script and write to popout.html
chrome.runtime.onMessage.addListener(function (message) {
document.getElementsByName("myName")[0].setAttribute("content", message);
});

payload.js:

// send the page title as a chrome message
chrome.runtime.sendMessage(document.getElementsByTagName("META")[0].content);

的manifest.json:

{
    "manifest_version": 2,

    "name": "MyName",
    "description": "empty!",
    "version": "0.2",
    "author": "me",

    "background": {
        "scripts": ["popup.js"],
        "persistent": true
    },

    "icons": { "16": "icon_16.png",
    "48": "icon_48.png",
    "128": "icon_128.png" },

    "permissions": [
        "tabs",
        "http://*/",
        "https://*/"
    ],
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    }
}

我已尝试将setInterval(function(){ code; }, 500);应用于popup.js和/或payload.js,但我无法工作。

2 个答案:

答案 0 :(得分:0)

创建一个background.js并将其放在manifest而不是popup.js中。

然后使用

从popup.html引用popup.js
import java.io.IOException;
import java.io.PrintWriter;

import org.json.simple.parser.ParseException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class JsoupDummy {
 public static void main(String[] args) throws IOException, ParseException {
    System.setProperty("webdriver.gecko.driver", "D:\\thirdPartyApis\\geckodriver-v0.19.1-win32\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();

    try {
        driver.get("https://chesstempo.com/chess-problems/15");
        Document doc = Jsoup.parse(driver.getPageSource());
        Elements elements = doc.select("span.ct-active-tag");
        for (Element element:elements){
             System.out.println(element.html());
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        /*write.flush();
        write.close();*/
        driver.quit();

    }
}
}

这样popup.js可以访问popup.html dom。

我认为这就是你需要做的一切,但没有时间去尝试。

答案 1 :(得分:0)

我通过更改popup.js修复了它。现在,在tab中加载站点完成后加载payload.js。这是我最后的popup.js:

 // Open https://mypage.com in current tab when popup has loaded
window.addEventListener('load', function (evt) {
    chrome.tabs.update(null, {url:"https://mypage.com"});

});

// Listen to messages from the payload.js script and write to popout.html
chrome.runtime.onMessage.addListener(function (message) {
document.getElementsByName("name")[0].value = message;
});

// Inject the payload.js script into the current tab after mypage.com has loaded
chrome.tabs.onUpdated.addListener(function (tabId , info) {
  if (info.status === 'complete') {
        chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
        file: 'payload.js'
    });
  }
});
相关问题