白左
千年狐狸
千年狐狸
  • UID34985
  • 注册日期2010-12-29
  • 最后登录2023-11-13
  • 发帖数2039
  • 经验655枚
  • 威望0点
  • 贡献值364点
  • 好评度69点
  • 社区居民
  • 忠实会员
阅读:2018回复:3

复制标签历史记录的脚本失效了,求修补【已解决】

楼主#
更多 发布于:2017-06-16 23:07
原来有个脚本,作用是打开新标签时保留来源标签的历史(源码见文末)

例如标签A为本论坛,历史是{第2页,第3页,第4页},长按“返回”按钮会显示三行记录,分别是2、3、4页
中键在新标签B打开“第5页”链接后,新标签B并不会继承来源标签A的历史记录,在标签B长按返回按钮不会有效果,利用fire gesture等方法强行显示的话,能看见历史只有{第5页}一项
脚本的作用就是使得标签B继承标签A的历史,新标签打开后显示历史为{第2页,第3页,第4页,第5页}


升级到55b1开发者版后,脚本失效,任何新打开的标签均抛出异常,例如拖拽打开新标签时的异常为:
Exception {
  message: "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsISHistoryInternal.addEntry]",
  result: 2147500037,
  name: "NS_ERROR_FAILURE",
  filename: "chrome://userchromejs/content/userChromeJS.js -> file:///K:/App/Pool/Fx/chrome/Main/RetainSessionHistoryInNewTab.js",
  lineNumber: 18,
  columnNumber: 0,
  data: null,
  stack: "copyHistory@chrome://userchromejs/content/userChromeJS.js -> file:///K:/App/Pool/Fx/chrome/Main/RetainSessionHistoryInNewTab.js:18:4loadOneTab@chrome://userchromejs/content/userChromeJS.js -> file:///K:/App/Pool/Fx/chrome/Main/RetainSessionHistoryInNewTab.js line 28 > eval:83:32DragToGo/this.DragDropHandler@chrome://userchromejs/content/userChromeJS.js -> file:///K:/App/Pool/Fx/chrome/Main/DragToGo.uc.js:221:10",
  location: XPCWrappedNative_NoHelper
}



一个在本机上可复现的测试脚本
var tabB = gBrowser.mCurrentTab;
var tabA = tabA.previousSibling;
var shB = tabB.linkedBrowser.sessionHistory.QueryInterface(Ci.nsISHistoryInternal);
var shA = tabA.linkedBrowser.sessionHistory;
shB.addEntry(shA.getEntryAtIndex(shA.index - 1, false), true);

1. 复制脚本到代码草稿纸内,切换为chrome运行环境
2. 新建标签A打开任意论坛(例如本论坛),点击右上角的翻页数字,依次点击2、3、4页。此时长按地址栏旁边“返回”按钮(←)应该能看见数行历史记录(sessionHistory)
3. 中键点击第5页数字链接,在新标签B打开第5页,并切换到新标签B。保证标签A、B相邻且A在B的左边。
4. 回到代码草稿纸,以“显示”模式(Ctrl+L)运行脚本


期望结果
无特殊显示,无异常。在标签B长按“返回”按钮出现新的历史记录。
实际结果
Exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsISHistoryInternal.addEntry]"  nsresult: "0x80004005 (NS_ERROR_FAILURE)"  location: "JS frame :: Scratchpad/2 :: <TOP_LEVEL> :: line 7"  data: no]



原脚本全文
// ==UserScript==
// @id             RetainSessionHistoryInNewTab #BAD
// @version        0.1.0.20130612
// @namespace      RetainSessionHistoryInNewTab@Byzod.UC.js
// @author         Byzod
// @modifiedfrom   ithinc
// @description    Retain Session History In New Tab
// ==/UserScript==

var SessionHistoryRetainer = {
    copyHistory : function(fromTab, newTab){
        try{
            
        let currentHistory = fromTab.linkedBrowser.sessionHistory;
        let newHistory = newTab.linkedBrowser.sessionHistory.QueryInterface(Ci.nsISHistoryInternal);
        /*TODO: linkedBrowser.sessionHistory is null*/
        for (let i = 0; i <= currentHistory.index; i++) {
            newHistory.addEntry(currentHistory.getEntryAtIndex(i, false), true);
        }
        
        } catch(ex){
            console.error(ex);
        }
    },
    
    initialize : function (e) {
        // Hack
        eval(
            "gBrowser.loadOneTab="
            + gBrowser.loadOneTab.toString()
                .replace("{", "{\nvar currentTab = this.mCurrentTab;")
                .replace("return tab;",
                        "if (aReferrerURI) {SessionHistoryRetainer.copyHistory(currentTab, tab);}"
                        + "\nreturn tab;")
        )
    },
};

if (window.location == "chrome://browser/content/browser.xul") {
    SessionHistoryRetainer.initialize();
}
-いたんですか? -ええ、ずっと
cinhoo
狐狸大王
狐狸大王
  • UID31676
  • 注册日期2010-01-21
  • 最后登录2019-01-08
  • 发帖数330
  • 经验15枚
  • 威望0点
  • 贡献值0点
  • 好评度6点
  • 社区居民
  • 忠实会员
1楼#
发布于:2017-08-09 12:46
应该跟 https://bugzilla.mozilla.org/show_bug.cgi?id=1363036 附件的 Part2 有关

newHistory.addEntry(currentHistory.getEntryAtIndex(i, false), true);
改成
let entry = currentHistory.getEntryAtIndex(i, false);
let newEntry = Cc["@mozilla.org/browser/session-history-entry;1"].createInstance(Ci.nsISHEntry);
newEntry.setURI(entry.URI);
newEntry.setTitle(entry.title);
newHistory.addEntry(newEntry, true);
【 在 hailongchang (novice) 的大作中提到: 】 上了Firefox这艘贼船就很难再下来了,我当年是报着试一试的心态用了下Firefox,当然包括当时一些如日中天的插件,这一用就抛弃IE了。几年过后,发现IE8也还不错,但是就是怎么都用不管,Firefox连同上面的那些插件已经彻底绑架了我的习惯,用别的浏览器觉得巨不爽,包括Chrome
白左
千年狐狸
千年狐狸
  • UID34985
  • 注册日期2010-12-29
  • 最后登录2023-11-13
  • 发帖数2039
  • 经验655枚
  • 威望0点
  • 贡献值364点
  • 好评度69点
  • 社区居民
  • 忠实会员
2楼#
发布于:2017-08-09 17:18
cinhoo:应该跟 https://bugzilla.mozilla.org/show_bug.cgi?id=1363036 附件的 Part2 有关

newHistory.addEntry(currentHistory.getEntryAtIn...
回到原帖
感谢cinhoo的指点!
修改之后运行正常,无报错!论坛果然卧虎藏龙
可惜即使修补好了脚本,脚本的生存土壤chromejs也要被死亡宣告了


虽然猜到复制新的entry可能有效,但是因为对组件API的不了解导致采用了错误的方法进行复制,无效后就没尝试了……果然专业的事还是得专业的人来才行
-いたんですか? -ええ、ずっと
白左
千年狐狸
千年狐狸
  • UID34985
  • 注册日期2010-12-29
  • 最后登录2023-11-13
  • 发帖数2039
  • 经验655枚
  • 威望0点
  • 贡献值364点
  • 好评度69点
  • 社区居民
  • 忠实会员
3楼#
发布于:2017-08-09 17:20
cinhoo:应该跟 https://bugzilla.mozilla.org/show_bug.cgi?id=1363036 附件的 Part2 有关

newHistory.addEntry(currentHistory.getEntryAtIn...
回到原帖
大佬爱你哟
-いたんですか? -ええ、ずっと
游客

返回顶部