Chrome扩展程序在点击时打开链接,并按类innertext过滤

时间:2017-03-29 13:18:31

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

我在扩展程序中使用chrome标签打开链接时遇到了麻烦,并通过某些类内部文本对其进行过滤。 我希望它打开所有以“http://www.ebay.com/usr/”开头的链接, 然后按类“mem_loc”过滤打开的窗口,如果它的innerText是例如“United States”(然后关闭它)。否则,如果它是假的,我希望它获得button id =“AnalyzeLink”的href属性并打开它。

的manifest.json:

{
  "name": "Asaf's Extension",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Asaf's ZIK Extension",
  "permissions": [
    "<all_urls>", "tabs", "activeTab"
  ],
  "background": {
  "matches": [ "*://*.zikanalytics.com/Analyze/Index/*" ],
  "scripts" : [ "event.js" ],
  "persistent": false
  },
    "browser_action": {
    "default_icon": "icon.png"
  },
  "content_scripts": [
    {
      "matches": [ "*://*.ebay.com/usr/*" ],
      "css": [ "ebayButton.css" ],
      "js": [ "ebayButton.js" ],
      "run_at": "document_end"
    },
    {
    "matches": [ "*://*.zikanalytics.com/Analyze/Index/*" ],
    "css": [ "ZIKeyword.css" ],
    "js": [ "ZIKeyword.js" ],
    "persistent": false
    }
  ]
}

event.js:

chrome.browserAction.onClicked.addListener(sellersList);

function sellersList(tab) {
    chrome.tabs.executeScript(tab.id, {file: 'sellersList.js'}, results => {
        results[0].forEach(openSeller);
    });
}

function openSeller(url) {
    chrome.tabs.create({url, pinned: true, active: true}, collectCountry);
}

function collectCountry(tab) {
    chrome.tabs.executeScript(tab.id, {file: 'collectCountry.js'}, results => {
        chrome.tabs.remove(tab.id);
        results[0].forEach(analyzeSeller);
    });
}

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

sellersList.js:

let urls = [...document.links]
    .filter(a => a.href.startsWith('http://www.ebay.com/usr/'))
    .map(a => a.href);
[...new Set(urls)];

collectCountry.js:

[...document.getElementsByClassName("mem_loc")]
.filter(a => a.textContent.trim().endsWith('s'))
.map(a=> document.URL)

它正在打开链接,并对其进行过滤,但如果找到匹配项,我不知道如何保存以前的网址,因此稍后会打开它。

提前致谢! :)

0 个答案:

没有答案