[英]SOLVED: Sending authenticated user's email from webpage to chrome extension
I'm creating a website (websiteA) with Laravel 8 and Vuejs and a scraping chrome extension with JavaScript. The extension is scraping the content of another websiteB and I want to store the scraped data from the websiteB to the authenticated user's row in the websiteA's database.我正在使用 Laravel 8 和 Vuejs 创建一个网站 (websiteA) 以及一个带有 JavaScript 的抓取 chrome 扩展。该扩展正在抓取另一个网站 B 的内容,我想将抓取的数据从网站 B 存储到网站 A 中经过身份验证的用户行数据库。 I'm stuck in getting the authenticated user's email or id from the websiteA to the chrome extension so the extension knows where to store the data.我坚持将经过身份验证的用户的 email 或 id 从 websiteA 获取到 chrome 扩展程序,以便扩展程序知道将数据存储在何处。
So far I've tried to send message from websiteA to the extension by defining the extension's ID and by using: chrome.runtime.sendMessage
and on the extension: chrome.runtime.onMessageExternal.addListener
到目前为止,我已经尝试通过定义扩展程序的 ID 并使用: chrome.runtime.sendMessage
和扩展程序: chrome.runtime.onMessageExternal.addListener
将消息从 websiteA 发送到扩展程序
but vue doesn't seem to recognise sendMessage and displays: "TypeError: Cannot read property 'sendMessage' of undefined"
但 vue 似乎无法识别 sendMessage 并显示: "TypeError: Cannot read property 'sendMessage' of undefined"
Why does vue show this error?为什么vue会显示这个错误?
Is there any other way?还有别的办法吗?
All I need is to pass the id or email from the websiteA to the chrome extension on websiteB.我只需要将 id 或 email 从 websiteA 传递到 websiteB 上的 chrome 扩展。
[Solved]: I used window.postMessage from the webpageA and window.addEventListener on the extension. [已解决]:我使用了网页A中的window.postMessage和扩展中的window.addEventListener。
Webpage (Vuejs):网页(Vuejs):
window.postMessage({ type: "FROM_PAGE", text: user_details }, "*");
content_script:内容脚本:
window.addEventListener("message", function(event) {
var user_details = event.data.text;
if (event.data.type && (event.data.type == "FROM_PAGE"))
{
{ ... }
chrome.runtime.sendMessage({'message': 'send_1' , 'data': user_details},function(response){ ... }
}
scraping_script:抓取脚本:
chrome.runtime.sendMessage({'message':'send_2', 'data': order},function(response){ ... });
background_script:背景脚本:
var user_details;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse){
if(request.message=="send_1")
{
user_details = request.data;
{ ... }
}
else if(request.message=='send_2')
{
send_data = user_details;
{ ...some post request... }
});
So the webpageA sends user_details to the content_script and the content_script sends them to the background script.所以网页A将user_details发送给content_script,content_script将它们发送给后台脚本。 As soon as the scraper does the scraping from the webpageB, sends the data to the background script.抓取器从网页 B 抓取数据后,立即将数据发送到后台脚本。 Then the background script does the post request sending the user_details and the scraped data to the webpageA.然后后台脚本执行发布请求,将 user_details 和抓取的数据发送到 webpageA。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.