Chrome extension run Javascript on X next tabs

我们两清 提交于 2020-01-01 20:25:15

问题


I'm making an extension and I can't understand how to do the following - My extensions currently open X amount of tabs, according to a specific text. Now I want it to run a specific script in every new tab it opens.

Manifest.json:

{
  "name": "Asaf Feedback Opener",
  "version": "1",
  "manifest_version" : 2,
  "description": "Opens any View Item on the current page",
  "background" : {
    "scripts" : ["background.js"]
  },
  "browser_action": {
    "default_icon": "icon.png"
  },
  "permissions": ["activeTab", "tabs"]
}

Now it runs the following code in the current page of clicking:

chrome.browserAction.onClicked.addListener(function(tab) {
   chrome.tabs.executeScript(null, {file: "testScript.js"});
});

Javascript:

var links = document.getElementsByTagName("a");
numofview = 0;

for (i = 0; i < links.length; i++)
{
    if (links[i].innerHTML.startsWith("View Item"))
{
        numofview = numofview+1;
        window.open(links[i].getAttribute("href"), "_blank");
}
}

Now, In the above JS It opens links in a new tab. I want the next code to run in every tab that the previous code had opened:

var soldlinks = document.getElementsByTagName("a");
for (i = 0; i < soldlinks.length; i++)
    {
    if (soldlinks[i].innerHTML.endsWith("sold"))
        {
        var soldlength = soldlinks[i].innerHTML.length
        var amount = soldlinks[i].innerHTML.substring(0,soldlength-5);
        if (Number(amount) >= 5)
            window.open(soldlinks[i].getAttribute("href"), "_self");
        else
            window.close()
            }
    }

I actually don't know JS or the chrome extensions coding language at all, I've been improvising with google to get this far.

Both scripts work in separate, I don't how to go forward from here.


回答1:


  1. Make your injected content scripts only collect and return the URLs to the background page script by leaving the data as the last expression in the file (see the code below). It will be available in executeScript's callback parameter which is an array of results per each frame in a tab so our data is inside its element 0 (main frame is 0 = the page itself).
  2. The background page script will open the tabs and use executeScript to inject the new content script into the opened tabs. This will require additional permissions in manifest.json for the opened sites or all URLs if you don't know which sites may be referred:

    "permissions": ["<all_urls>", "tabs"]
    
  3. Use an event page that's automatically unloaded when your extension is not used.

    "background" : {
        "scripts" : ["event.js"],
        "persistent": false
    },
    

event.js (previously background.js now renamed for clarity):

chrome.browserAction.onClicked.addListener(collectViewItems);

function collectViewItems(tab) {
    chrome.tabs.executeScript(tab.id, {file: 'collect-view-items.js'}, results => {
        results[0].forEach(openViewItem);
    });
}

function openViewItem(url) {
    chrome.tabs.create({url, pinned: true, active: false}, collectSoldItems);
}

function collectSoldItems(tab) {
    chrome.tabs.executeScript(tab.id, {file: 'collect-sold-items.js'}, results => {
        chrome.tabs.remove(tab.id);
        results[0].forEach(openSoldItem);
    });
}

function openSoldItem(url) {
    chrome.tabs.create({url, active: false});
}

collect-view-items.js:

[...document.links]
    .filter(a => a.textContent.trim().startsWith('View Item'))
    .map(a => a.href);

collect-sold-items.js:

[...document.links]
    .filter(a => a.textContent.trim().endsWith('sold') && parseInt(a.textContent) >= 5)
    .map(a => a.href);

ES2015 syntax is used, available in modern browsers by default.



来源:https://stackoverflow.com/questions/42330245/chrome-extension-run-javascript-on-x-next-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!