gpt4 book ai didi

javascript - 消息传递在 Chrome 扩展中不起作用

转载 作者:行者123 更新时间:2023-12-03 08:04:01 25 4
gpt4 key购买 nike

我尝试将消息从 Default_popup.js 传递到内容脚本,当在内容脚本末尾接收到消息时,尝试将数据保存在 Chrome 存储上。但我的代码无法正常工作。

该代码运行了 2-3 次。但现在它不起作用。

list .json

{
"manifest_version": 2,

"name": "Test",
"description": "Automated Test Tool.",
"version": "1.0",

"browser_action": {
"default_icon": "icon.png",
"default_popup": "Default_Popup.html"
},
"content_scripts": [{
"matches":["http://*/*","https://*/*"],
"js":["myscript.js"] }
],
"background":{
"scripts":["background.js"]
},

"permissions":[
"storage",
"notifications",
"tabs",
"http://*/",
"https://*/"
]
}

Popup.js

chrome.tabs.getSelected(null, function (tab) {
var Jour = {};
Jour.FromStation = $('#txtFromStation').val();
Jour.ToStation = $('#txtToStation').val();
Jour.JourneyDate = $('#datepicker').val();
chrome.tabs.sendRequest(tab.id, { JourneyDetails: Jour }, function handler(response) {
alert("Inside Client = " + "Done");
});
});

myscript.js//内容脚本

window.onload = function () {

chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
alert('request.JourneyDetails.FromStation');
alert(request.JourneyDetails.FromStation);
var Jour = {};
Jour.FromStation = request.FromStation;
Jour.ToStation = request.ToStation;
Jour.JourneyDate = request.JourneyDate;
chrome.storage.sync.set({ JourneyDetails: Jour }, function () {
console.log('Setting Saved')
});
//sendResponse({ counter2: "5" });
}
);
}

最佳答案

  1. chrome.tabs.getSelected() 已弃用,尝试改用 chrome.tabs.query() 吗? https://developer.chrome.com/extensions/tabs#method-getSelected
  2. chrome.tabs.sendRequest()chrome.extension.onRequest() 已弃用,请尝试使用 chrome.tabs.sendMessage() > 和 chrome.runtime.onMessage() 代替? https://developer.chrome.com/extensions/tabs#method-sendRequest , https://developer.chrome.com/extensions/extension#event-onRequest
  3. 您是否在 Default_Popup.html 中包含 jQuery?您看到了哪些错误?
  4. Default_Popup.html 中的 ID 为 $('#txtFromStation')、$('#txtToStation')、$('#datepicker') 的元素吗?如果您可以发布 Default_Popup.html 的内容,那将会很有帮助。

关于javascript - 消息传递在 Chrome 扩展中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34468836/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com