尝试创建Chrome扩展程序,该扩展程序将从浏览器选项卡A复制数据并将其填写在浏览器选项卡B的表单中

m1x*_*1an 2 javascript google-chrome google-chrome-extension

基本上,我试图从<input>s选项卡上的复制数据,然后<input>使用chrome扩展名将其粘贴到另一选项卡上的表单s中。

我要弄清楚的部分是如何将数据从一个选项卡传输到另一个选项卡,以及如何定位正确的选项卡。

从表单输入中复制数据很简单document.getElementById(),我创建了一个content.js,当单击扩展图标时,它会在当前选项卡上运行。

表现

{
    "name": "AnalystWizard",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Helping analysts since 2016",
    "background" : {
           "scripts" : ["background.js"]
        },
    "permissions": [
        "tabs", "<all_urls>"
    ],
    "browser_action": {
        "default_icon": "icon.png"
    }
}
Run Code Online (Sandbox Code Playgroud)

Background.js

chrome.browserAction.onClicked.addListener(function(tab) {
   chrome.tabs.executeScript(null, {file: "content.js"});
});
Run Code Online (Sandbox Code Playgroud)

Content.js

var name = document.getElementById("fname").value;
var street = document.getElementById("street").value;
var state = document.getElementById("state").value;
Run Code Online (Sandbox Code Playgroud)

在这一点必要的数据存储在内部namestreetstate变量。这是我遇到麻烦的地方。将数据存储在变量中后,如何告诉扩展程序允许用户定位新标签页?插入数据我可以做与上述相同的操作,但可以相反。

新标签:

document.getElementById("form1_name").value = name;
document.getElementById("form1_street").value = street;
document.getElementById("form1_state").value = state;
Run Code Online (Sandbox Code Playgroud)

也许我可以popup.html从扩展程序中使用一种方法,该方法将有两个按钮,一个标记为source将在当前选项卡上抓取数据,然后用户将更改为他们想要粘贴的选项卡,第二个按钮destination将作为输入新活动标签上的数据?

m1x*_*1an 5

我最终通过使用storage许可和 解决了这个问题chrome.storage.local.set()。无需传递消息。用户点击上的来源按钮,该按钮popup.html将从当前活动标签中抓取数据并将其存储在本地chrome.storage。然后,用户可以导航到他们想要粘贴数据的页面,然后单击弹出窗口上的“目标”按钮,然后从中回调数据chrome.storage

manifest.json

{
    "name": "MyWizard",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Scrape data to new form",
    "permissions": [
        "tabs", "<all_urls>",
        "storage"
    ],
    "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
    }
}
Run Code Online (Sandbox Code Playgroud)

Popup.html

<html>
<body>
<button type="button" id="btnSource">Source Page</button><br>
<button type="button" id="btnTarget">Target Page</button>
<script src="popup.js"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

popup.js

function doContent(){
chrome.tabs.executeScript(null, {file: "content_nomsg.js"});
};

function doTarget(){
chrome.tabs.executeScript(null, {file: "content2.js"});
};

document.getElementById("btnSource").onclick = doContent;
document.getElementById("btnTarget").onclick = doTarget;
Run Code Online (Sandbox Code Playgroud)

content_nomsg.js

var fname = document.getElementById("j_id0:frmApp:frmAppBlock:j_id128:j_id130:0:j_id132:j_id133").value;
var lname = document.getElementById("j_id0:frmApp:frmAppBlock:j_id128:j_id130:0:j_id132:j_id134").value;

console.log("source page ran");

var storArray = {
    src_fname: fname,
    src_lname: lname
    };

chrome.storage.local.set({
        'newStorage': storArray
        });
Run Code Online (Sandbox Code Playgroud)

content2.js

console.log("target content2 ran");

var storedLegal = chrome.storage.local.get('newStorage', function (items) {
    console.log(items); 

    document.getElementById("firstName").value = items.newStorage.src_fname;
    document.getElementById("lastName").value = items.newStorage.src_lname; 
  });
Run Code Online (Sandbox Code Playgroud)