尝试将事件从chrome扩展添加到角度元素

时间:2019-05-06 13:12:57

标签: angular google-chrome-extension

我正在构建chrome扩展程序,该扩展程序可识别IP地址并从外部API获取一些报告。 该扩展程序可以在常规HTML网站上正常运行,但是当我尝试在Angular网站上执行相同操作时,无法访问angular元素。 我的目标是用一个具有特定属性的特殊span标签包装每个IP地址正则表达式(在角度元素旁边工作正常)。

manifest.json

{
    "name": "IP information on hover",
    "description": "IP information on hover",
    "version": "1.0",
    "manifest_version": 2,
    "browser_action": {
        "default_icon": "hello_extensions.png"
    },
    "permissions": [
        "https://www.virustotal.com/",
        "http://www.virustotal.com/"
    ],
    "background": {
        "scripts": [
            "background.js",
            "utils.js"
        ],
        "persistent": false
    },
    "content_scripts": [
        {
            "matches": [
                "http://*/*",
                "https://*/*"
            ],
            "js": [
                "jquery-3.4.0.js",
                "contentScript.js"
            ],
            "css": [
                "style.css"
            ]
        }
    ]
}

contentScript.js

let ipRegEx = new RegExp(/\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b/g);

console.log('Idan Asulin');
let elements = $("*").filter(function () {
    return ipRegEx.test($(this).text());
});

elements.each((index, element) => {                    // Iterates all elements which contains ip
    let content = $(element).text();
    let ipsArray = content.match(ipRegEx);
    ipsArray.forEach(ipAddress => {                    // Iterates all IPs in that element 
        content = content.replace(ipAddress, `<span class="ipAddress">${ipAddress}</span>`);
        $(element).html(content);
    });
});

$(".ipAddress").mouseover(event => {
    chrome.runtime.sendMessage({ data: event.target.innerHTML }, ipReport => {
        if (ipReport !== 'Error') {
            let title = `Malicious: ${ipReport.malicious},\nPositive downloads: ${ipReport.positiveDownloads},\nPositive URLs: ${ipReport.positiveURLs},\nAs owner: ${ipReport.asOwner}`;
            title = title.replace(/[ ]/g, "\u00a0");
            if (ipReport.malicious === true)
                event.target.setAttribute('data-title-malicious', title);
            else
                event.target.setAttribute('data-title-clean', title);
        }
    });
})

$('*').click( () => {
    console.log('Success');
    $(this).attr('idan', 'red');
})

backgroundScript.js

chrome.runtime.onMessage.addListener((message, sender, callback) => {
    fetch(`http://www.virustotal.com/vtapi/v2/ip-address/report?ip=${message.data}&apikey=MY API KEY`, {
        strictSSL: false
    })
    .then(res => res.json())
    .then(data => {
        callback({
            malicious: data.response_code > 0,
            positiveDownloads: calcDownloads([data.detected_downloaded_samples, data.undetected_downloaded_samples]),
            positiveURLs: calcUrls(data.detected_urls),
            asOwner: data.as_owner
        });
    })
    .catch(() => {
        callback('Error');
    })
    return true;
});

最后,我想要做的就是让Ip正则表达式自动换行出现在角元素中

0 个答案:

没有答案