简体   繁体   English

Chrome扩展-打开新标签并在加载DOM时插入数据

[英]Chrome extenstion - Open new tab and insert data when DOM loaded

i want to ask about issue which i had while i want to create my own chrome extension. 我想问一下我想创建自己的chrome扩展程序时遇到的问题。 I want to create extension, for my blog to fill the article form automatically. 我想创建扩展名,以便我的博客自动填写文章表格。

I want to count articles, and then ( due article count ) open tab, paste data, open tab, paste second data ... 我要计算文章数,然后(应计文章数)打开标签页,粘贴数据,打开标签页,粘贴第二个数据...

I created manifest, background.js and content.js files. 我创建了manifest,background.js和content.js文件。

After icon click i start background.js ( count articles, select data and paste to array - everything works good), but at the time, when i want to open new tab with url ( eg www.google.com ) and after DOM of tab is loaded, paste data to input and when the pasting is complete, i want to open new tab and repeat the action. 单击图标后,我启动background.js(计数文章,选择数据并粘贴到数组-一切正常),但是当时,我想使用url打开新标签页(例如www.google.com),之后是DOM选项卡已加载,将数据粘贴到输入中,并且粘贴完成后,我想打开新的选项卡并重复操作。

But, my issue is: "how to check when the tab DOM is loaded and when the script finish pushing data and call open new tab and repeat." 但是,我的问题是:“如何检查标签DOM何时加载以及脚本何时完成推送数据并调用打开新标签并重复。”

Any ideas how to solve this? 任何想法如何解决这个问题? Thanks a lot. 非常感谢。

Btw sorry for my english... :-/ 顺便说一句,抱歉我的英语...:-/

You can use chrome.tabs.onUpdated . 您可以使用chrome.tabs.onUpdated

chrome.tabs.onUpdated has a callback with parameter changeInfo . chrome.tabs.onUpdated具有带有参数changeInfo的回调。 and changeInfo include status. changeInfo包含状态。

I think below code might works. 我认为以下代码可能有效。 (didn't run it) (没有运行)

chrome.tabs.onUpdated(function(id, changeInfo) {
  if (changeInfo.status && changeInfo.status == "complete")
    // insert data
});

Thanks for your answer, but i created this simple script: 感谢您的回答,但我创建了以下简单脚本:

chrome.browserAction.onClicked.addListener(function(tab) {
   var action_url = "http://google.com";
   chrome.tabs.create({ url: action_url });
   run(tab.id);
});function run(tabId){
     chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
     alert("new tab id is: " +tabId);

     if (info.status && info.status == "complete"){
         alert("complete");
         document.body.innerHTML = "";
     }
   });
}

But the ALERT is still runed early than dom of page is loaded, and the next java script code is not runed, because the dom isnt exist yet... this is still same problem. 但是ALERT仍在加载页面dom之前运行,并且下一个java脚本代码未运行,因为dom还不存在...这仍然是相同的问题。

PS: i used google as example, when u use some "bigger" site, u can see the alert run before whole page is loaded PS:我以google为例,当您使用某个“更大”的网站时,可以在整个页面加载之前看到警报运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM