Tag page ID changes in Chrome extension development

Source: Internet
Author: User

The whole process is probably like this:
Background.js monitors the Click events on the page, creates a new tab, and assigns the ID value of the New tab page to a global variable tasktab:

The code is as follows Copy Code
var Tasktab;
Chrome.tabs.create ({url:/blog/}, function (tab) {
Tasktab = tab.id;
});

Also, monitor webnavigation oncommitted events in background.js for dynamic injection of content scripts:

  code is as follows copy code
chrome.webnavigatio N.oncommitted.addlistener (function (details) {
if (details.tabid = = tasktab) {
    Chrome.tabs.executeScripts (tasktab, {file: ' Example.js '});
}
});

After the new tab opens and injects the content script, the content script finds the link "http://www.zfanw.com/blog/about" in the page and simulates the click.
and assume that the new tab page HTML code also contains the following:
<link rel= "PreRender" href= "/about"
Prerender1 is an accelerated access feature of the Chrome browser. It can be when we visit a page, the background load good b page, so we visit the B page, very fast.
Extract a description of link 1:
A hidden page is created for the prerendered URL, which'll do full loading to all dependent Ces, as as, execution of Javascript. If the user navigates to the page, the hidden page is swapped into the current tab and made visible. The
B page is loaded in a hidden page, and when we click on the b link, the hidden page is placed on the current tab and is visible.
literally, there is no word to imply that the tab ID will change.
But in fact, the tab ID does change, and this can be checked by binding a onreplaced event in the background.js. The
OnReplaced2 event is described as follows:

The code is as follows Copy Code
Fired when the contents's tab is replaced by a different (usually previously pre-rendered).
The binding code is as follows:
Chrome.tabs.onReplaced.addListener (function (Addedtabid, removedtabid) {
Console.log "added TabID:" + addedtabid;
Console.log "Removed tabid:" + removedtabid;
});

By opening the developer tool for the background page, we can see that when the analog click event occurs, the Chrome browser removes a tab and adds one. Just from the naked eye, we don't see the replacement process at all.
In my case, because the tab ID changes, so the value of tasktab is actually useless, resulting in the click on the open about page can not inject content script. If you also encounter the failure of content script dynamic injection, you may wish to check the ID value of the label page below.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.