我在两个网站上使用Chrome开发人员工具控制台执行脚本。在第一个网站上,脚本收集数据,在第二个网站上,另一个脚本获取这些数据,并将其放入表格中。
网站使用不同的域名。目前我正在手动复制脚本一的结果,并将其作为输入数据粘贴到脚本二中。但是这需要花费太多的时间。
如何通过编程将数据从第一个网站的控制台传递到第二个网站的控制台,知道它们不是同源的?
发布于 2020-12-14 13:06:10
我建议使用userscript代替。设置在两个页面上运行的用户脚本,使用GM.setValue保存第一个页面上的数据,然后使用GM.getValue检索第二个页面上的数据。例如:
// ==UserScript==
// @name New Userscript
// @include /^https://stackoverflow.com/
// @include /^https://example.com/
// @grant GM.setValue
// @grant GM.getValue
// ==/UserScript==
if (window.location.host === 'stackoverflow.com') {
GM.setValue('data', 'foo'); // .catch the Promise here if you want
// after the data is saved, you could also redirect automatically if you want:
// window.location.href = 'example.com'
} else {
GM.getValue('data')
.then((result) => {
console.log(result);
});
// .catch the Promise here if you want
}结果,在转到堆栈溢出,然后转到example.com之后:

你需要一个像Tampermonkey这样的用户脚本管理器。
不再需要复制和粘贴-只需编写一次用户脚本代码,即可自动抓取、保存,可能还会重定向到新页面并填充它。
https://stackoverflow.com/questions/65283835
复制相似问题