我如何将chrome扩展名中的选定文本从后台脚本传递到内容脚本?

问题描述 投票:1回答:1

我正在构建扩展程序,它本质上是窃检查程序。因此,您复制文本,然后在将您定向到我们的网站后单击鼠标右键。现在,我要做的是将选定的文本从复制的网站发送到网站输入页面,然后单击“提交”按钮。为此,我需要执行这两行。

    document.getElementById("mycontent").value = "selected text";  
    document.getElementById("checkButton").click();    

但是选定的文本仅保留在background.js中,并且从不显示在内容脚本中,这就是我的扩展名不起作用的原因。因此,我想知道如何解决此问题,或者还有其他方法可以输入文本并单击按钮。Background.js

  var contextsList = ["selection"];

    for(i = 0;i<contextsList.length; i++){
        var context = contextsList[i];
        var titleX = " Check Plagiarism of Selected Text";
        chrome.contextMenus.create({title: titleX, contexts:[context], onclick: clickHandler, id: context });
    }
  function clickHandler(data, tab) {
         switch(data.menuItemId.id){
            case 'selection' :
             ex = encodeURIComponent(data.selectionText);
 var config = {content: ex};
chrome.tabs.executeScript(tab.id, {
    code: 'var config = ' + JSON.stringify(config)
}, function() {
    chrome.tabs.executeScript(tab.id, {file: 'contentScript.js'});
});
// background script
  chrome.runtime.onMessage.addListener((request,sender,sendMessage)=>{

if(request.type==="Getcontent"){
    const  content=// set your content 
    sendMessage({msg:"cont",content:content})

}

})

chrome.tabs.create({url: "https://greatseotools.net/plagiarism-checker"});


   break;

            }

         }

ContentScript.js

        //Content script 
chrome.runtime.onMessage.addListener((request,sender,sendMessage)=>{

    if(request==='content'){
     console.log("content",request.content)       
    }

})

//wrap this in an event 
chrome.runtime.sendMessage({
    type:'Getcontent'
})

manifest.json

    {
   "background": {
      "scripts": [ "background.js" ]
   },
   "browser_action": {
      "default_icon": "icons/19.png",
      "default_popup": "popup.html"
   },
   "content_scripts": [ {
      "all_frames": true,
      "js": ["contentScript.js" ],
      "matches": [ "*://*.greatseotools.net/*" ],
      "run_at": "document_end"
   } ],
   "description": "Check Plagiarism by just selecting text..",
   "homepage_url": "https://www.prepostseo.com/plagiarism-checker",
   "icons": {
      "128": "icons/128.png",
      "16": "icons/16.png",
      "48": "icons/48.png"
   },
    "manifest_version": 2,
   "name": "Plagiarism Checker for Chrome",
   "permissions": [ "activeTab","*://*/*", "https://ajax.googleapis.com/", "contextMenus" ],
   "update_url": "https://clients2.google.com/service/update2/crx",
   "version": "1.0"
}

我必须在其上输入文本并单击按钮的网站。

https://greatseotools.net/plagiarism-checker
javascript jquery google-chrome-extension
1个回答
0
投票

在这一行chrome.tabs.executeScript({file: 'contentScript.js'})中,您没有将您的selectionText传递给内容脚本,您可以确保在很多方面,这是我的两个建议:

后台脚本

1。使用chrome.tabs.executeScript API

 ex = encodeURIComponent(data.selectionText);
 var config = {content: ex};
chrome.tabs.executeScript(tab.id, {
    code: 'var config = ' + JSON.stringify(config)
}, function() {
    chrome.tabs.executeScript(tab.id, {file: 'content.js'});
});

内容脚本

并且在内容脚本中,您应该可以检索ex:

alert('mycontent:' + config);

2。在背景和内容脚本之间使用chrome消息api

后台脚本

  // background script
  chrome.runtime.onMessage.addListener((request,sender,sendMessage)=>{

if(request.type==="Getcontent"){
    const  content=// set your content 
    sendMessage({msg:"cont",content:content})

}

})

内容脚本

//Content script 
chrome.runtime.onMessage.addListener((request,sender,sendMessage)=>{

    if(request==='content'){
     console.log("content",request.content)       
    }

})

//wrap this in an event 
chrome.runtime.sendMessage({
    type:'Getcontent'
})
© www.soinside.com 2019 - 2024. All rights reserved.