我已经准备好嵌入的 Instagram 媒体数据数组。每个 html 的末尾都有一个脚本 <script async defer src="//platform.instagram.com/en_US/embeds.js"></script>
.
默认情况下,Electron 将此链接指向 file://
协议(protocol)。添加http://
在src中正确返回js文件。
所以链接<script async defer src="http://platform.instagram.com/en_US/embeds.js"></script>
工作正常。
除了解析数据和重写链接之外,如何解决这个问题?
最佳答案
您面临的问题是 //
是一个协议(protocol)相对 URL,它将使用请求它的文件的任何协议(protocol)。您可以阅读更多相关信息 here .
覆盖此默认行为的最佳选择是解析数据并使用正则表达式查询之类的内容重写链接。
或者你可以尝试拦截文件协议(protocol),验证该url是你想要拦截的,然后重新格式化url,你可以了解如何做到这一点here 。下面的示例不包括您希望拦截的路径的验证。
const {app, protocol} = require('electron')
const path = require('path')
app.on('ready', () => {
protocol.registerFileProtocol('file', (request, callback) => {
const url = request.url.substr(7)
callback({path: path.normalize(`http://${__dirname}/${url}`)})
}, (error) => {
if (error) console.error('Failed to register protocol')
})
})
关于javascript - 在 Electron 中嵌入外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48488785/