Window pdf 加载后标题没有改变
Window title is not changed after pdf is loaded
我正在创建一个 chrome 扩展,它可以更改某些 pdf 页面的标题,例如 https://arxiv.org/pdf/2005.01463v2.pdf。
代码在 加载 pdf 之前工作正常,但是当 pdf 完全加载时,window 的标题被改回 pdf 名称(而不是我的头衔)和使用 document.title = "cool title"
或 Chrome 消息到 background.js
不起作用。
然而,在给定的 pdf 页面中从控制台执行手册 document.title = "cool title"
效果很好。
// content_script.js
const makeTitle = (time) => {
setTimeout(() => {
title = "cool title"
console.log("Updating pdf title");
chrome.runtime.sendMessage({ type: "update-title", options: { title } })
window.document.title = title;
}, time)
}
makeTitle(0)
makeTitle(1 * 1000);
makeTitle(5 * 1000);
makeTitle(10 * 1000);
makeTitle(20 * 1000);
makeTitle(60 * 1000);
makeTitle(5 * 60 * 1000);
// background.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log("Executing background message")
if (request.type == "update-title") {
const { title } = request.options;
chrome.tabs.executeScript(sender.tab.id, { code: `document.title = "${title}"` });
}
});
我必须使用那些 setTimeout
因为我无法检测 pdf 是否已完成加载。 makeTitle
在加载 pdf 之前工作正常,但在加载之后就不行了,我想解决这个问题。谢谢!
PS:我知道有一个重复的标题更改,只有 none 这两个策略起作用
这是 Chrome 中的错误。它在内部设置标题并忽略 DOM title
元素。
解决方法是使用 chrome.tabs.onUpdated 设置一个空标题,然后设置预期的标题。
background.js
const title = 'foo';
chrome.tabs.onUpdated.addListener((tabId, info, tab) => {
if (info.title && info.title !== title && tab.url.endsWith('.pdf')) {
chrome.tabs.executeScript(tabId, {
code: `document.title=''; document.title="${title}"`,
runAt: 'document_start',
// runs immediately if this change occurs before DOMContentLoaded
});
}
});
Chrome 中还有另一个错误:当您切换到另一个选项卡并返回时,它会间歇性地显示 built-in 标题。这个特殊的错误似乎在即将推出的“未经处理的”PDF 查看器中得到修复,目前可以通过 chrome://flags/#pdf-unseasoned
启用
我正在创建一个 chrome 扩展,它可以更改某些 pdf 页面的标题,例如 https://arxiv.org/pdf/2005.01463v2.pdf。
代码在 加载 pdf 之前工作正常,但是当 pdf 完全加载时,window 的标题被改回 pdf 名称(而不是我的头衔)和使用 document.title = "cool title"
或 Chrome 消息到 background.js
不起作用。
然而,在给定的 pdf 页面中从控制台执行手册 document.title = "cool title"
效果很好。
// content_script.js
const makeTitle = (time) => {
setTimeout(() => {
title = "cool title"
console.log("Updating pdf title");
chrome.runtime.sendMessage({ type: "update-title", options: { title } })
window.document.title = title;
}, time)
}
makeTitle(0)
makeTitle(1 * 1000);
makeTitle(5 * 1000);
makeTitle(10 * 1000);
makeTitle(20 * 1000);
makeTitle(60 * 1000);
makeTitle(5 * 60 * 1000);
// background.js
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log("Executing background message")
if (request.type == "update-title") {
const { title } = request.options;
chrome.tabs.executeScript(sender.tab.id, { code: `document.title = "${title}"` });
}
});
我必须使用那些 setTimeout
因为我无法检测 pdf 是否已完成加载。 makeTitle
在加载 pdf 之前工作正常,但在加载之后就不行了,我想解决这个问题。谢谢!
PS:我知道有一个重复的标题更改,只有 none 这两个策略起作用
这是 Chrome 中的错误。它在内部设置标题并忽略 DOM title
元素。
解决方法是使用 chrome.tabs.onUpdated 设置一个空标题,然后设置预期的标题。
background.js
const title = 'foo';
chrome.tabs.onUpdated.addListener((tabId, info, tab) => {
if (info.title && info.title !== title && tab.url.endsWith('.pdf')) {
chrome.tabs.executeScript(tabId, {
code: `document.title=''; document.title="${title}"`,
runAt: 'document_start',
// runs immediately if this change occurs before DOMContentLoaded
});
}
});
Chrome 中还有另一个错误:当您切换到另一个选项卡并返回时,它会间歇性地显示 built-in 标题。这个特殊的错误似乎在即将推出的“未经处理的”PDF 查看器中得到修复,目前可以通过 chrome://flags/#pdf-unseasoned