简体   繁体   English

Chrome 扩展程序(无法获取和单击新 Chrome 选项卡上的元素)

[英]Chrome extensions (can't get and click elements on new chrome tabs)

That's it.而已。 Lemme explaim myself.让我解释一下自己。 I coded a chrome extension that when clicking on a button, it will open a new resized tab (a paypal login), but I can't manage to click the "log in button" of paypal because trying to我编写了一个 chrome 扩展程序,当单击按钮时,它将打开一个新的调整大小的选项卡(paypal 登录),但我无法单击 paypal 的“登录按钮”,因为试图

document.getElementById('btnLogin').click();

isn't working, neither adding that:不起作用,也没有添加:

var paypal = window.open("https://www.paypal.com/es/signin", "PayPal", "width=400,height=400,location=yes,menubar=yes,status=yes,titilebar=yes,resizable=yes");
paypal.onload = function() {
    paypal.document.getElementById('btnLogin').click();
}

Check out all of my code:查看我的所有代码:

manifest.json manifest.json

{
    "manifest_version": 2,
    "name": "Zapatillao",
    "description": "Yatusabe te cojo sapatilla",
    "version": "2.0.0",
    "content_scripts": [
        {
        "matches": ["<all_urls>"],
        "js": ["content.js"]
        }
    ],
    "browser_action": {
        "default_popup": "popup.html",
        "default_icon": "icon128.jpg"
    },
    "permissions": [
        "tabs",
        "http://*/*",
        "https://*/*",
        "chrome://*/*"
    ]
}

content.js内容.js

document.getElementById('paypal_prelog').addEventListener('click', function() {
    chrome.tabs.executeScript({
      file: "paypal_prelog.js"
    });
});

popup.html popup.html

<html>
    <head>
        <style>
            html, body {
                height: 200px;
                width: 200px;
            }
        </style>
    </head>
    <body>
        <h1>Discord: Loan#2334</h1>
        <button id='paypal_prelog'>PayPal Prelogin</button>
        <script src='content.js'></script>
    </body>
</html>

paypal_prelog.js paypal_prelog.js

function sleep (ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

var paypal = window.open("https://www.paypal.com/es/signin", "PayPal", "width=400,height=400,location=yes,menubar=yes,status=yes,titilebar=yes,resizable=yes");
paypal.onload = function() {
    paypal.document.getElementById('btnLogin').click();
}

function closeWin() {
    paypal.close();
}

I have tried thousand of things, hope you can help me.我已经尝试了数千种方法,希望您能帮助我。

The solution for a ManifestV2 extension, which you are writing, in short, is to open a new paypal sign-in window using chrome.windows.create and then use chrome.tabs.executeScript to inject a content script code that clicks btnLogin . The solution for a ManifestV2 extension, which you are writing, in short, is to open a new paypal sign-in window using chrome.windows.create and then use chrome.tabs.executeScript to inject a content script code that clicks btnLogin . Your current code does it all wrong though.但是,您当前的代码完全错误。

  1. Remove content_scripts , tabs , and chrome://*/* from manifest.json.从 manifest.json 中删除content_scriptstabschrome://*/*

  2. Remove content.js from your extension and popup.html从您的扩展和 popup.html 中删除content.js

  3. Remove paypal_prelog.js删除paypal_prelog.js

  4. Create popup.js创建popup.js

     document.getElementById('paypal_prelog').onclick = () => { chrome.windows.create({ url: 'https://www.paypal.com/es/signin', width: 400, height: 400, }, w => { chrome.tabs.executeScript(w.tabs[0].id, { code: `(${() => { document.getElementById('btnLogin').click(); }})()`, }); }); };
  • load popup.js in popup.html:在 popup.html 中加载 popup.js:

     <script src="popup.js"></script> </body>

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

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