vsDizzy / SaveAsMHT

Chrome extension saving page as .mht
https://chrome.google.com/webstore/detail/save-as-mht/hfmodljjaibbdndlikgagimhhodmobkc
75 stars 23 forks source link

Resave error #30

Open Antiever opened 1 month ago

Antiever commented 1 month ago

Previously, I used the extension all the time to edit previously saved mht files, but on the latest versions, the save file window doesn't appear.

Error log

chrome.action.onClicked.addListener(function (tab) {
    saveTab(tab);
});
chrome.runtime.onInstalled.addListener(function () {
    chrome.contextMenus.create({
        id: 'save-mht',
        title: 'Save as .mht...',
        contexts: ['all'],
    });
});
chrome.contextMenus.onClicked.addListener(function (info, tab) {
    if (info.menuItemId == 'save-mht') {
        saveTab(tab);
    }
});
async function saveTab(tab) {
    chrome.downloads.download({
        conflictAction: 'prompt',
        filename: `${sanitizeFilename(tab.title)}.mht`,
        saveAs: true,
        url: await captureTabToDataUrl(tab.id),
    });
}
function sanitizeFilename(filename) {
    return filename.replace(/[<>:"/\\|?*\x00-\x1F~]/g, '_');
}
async function captureTabToDataUrl(tabId) {
    const tabText = await new Promise(function (resolve, reject) {
        chrome.pageCapture.saveAsMHTML({ tabId }, function (mhtmlData) {
            return mhtmlData
                ? resolve(mhtmlData.text())
                : reject(new Error(chrome.runtime.lastError.message));  <------ error
        });
    });
    return `data:multipart/related;base64,${btoa(tabText)}`;
}
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab) {
    if (changeInfo.status != 'loading') {
        return;
    }
    setPopup(tabId, tab.url);
});
chrome.tabs.query({}).then(function (tabs) {
    tabs.forEach(function (tab) {
        setPopup(tab.id, tab.url);
    });
});
async function setPopup(tabId, url) {
    if (!/^file:\/\/\/.*\.mht(ml)?$/i.test(url)) {
        return;
    }
    const { text, popup } = (await chrome.permissions.contains({ origins: ['file:///*'] }))
        ? { text: '🛈', popup: 'mht-info.html' }
        : { text: '⚠', popup: 'file-permission.html' };
    chrome.action.setBadgeText({ tabId, text });
    chrome.action.setPopup({ tabId, popup });
}
export {};
//# sourceMappingURL=worker.js.map

Permissions to work with local files are granted

chrome_anG0ZF0wkI

vsDizzy commented 1 month ago

Thanks for reporting this. I am able to reproduce this. The actual error is

Error: Don't have permissions required to capture this page.

This is something that is changed in Chrome permissions.

As a workaround you can currently use "save as..." in the context menu, image

Antiever commented 1 month ago

Thank you. AFAIK "save as" can't save page edited with devtools, it's just grab original code :( Can you specify the version of chrome from which this limitation appeared?

vsDizzy commented 1 month ago

I'm not sure. I see it has been reported since 2019. https://github.com/brian-girko/save-as-mhtml/issues/4 Also this https://www.google.com/search?q=%22Don%27t+have+permissions+required+to+capture+this+page.%22

vsDizzy commented 1 month ago

I opened Chromium ticket