【问题标题】:Wikipedia quick cite page bookmark维基百科快速引用页书签
【发布时间】:2019-03-14 21:34:13
【问题描述】:

我想做一个书签,功能与 to this one on 9xbuddy 类似,但我希望它用于 Wikipedia 上的“引用此页”功能。这就是我所拥有的:

javascript:(function(){ window.open('https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page=/'+ document.location.href); })();

但如果你要尝试使用这个东西,它几乎可以工作。问题在于页面 ID。所以我想知道的是,是否可以让书签获取它们?

在此先感谢(也可以运行下面的 sn-p 以轻松测试它;这样您就可以确切地看到使用书签时的结果)。

$(".selectable").click(function() {
    $(this).select();
});
$(document).ready(function () {
    var selectcounter = 1;
    
    $(".selectable").each(function() {
        idja = "selectable" + selectcounter;
        $(this).attr('id', idja);
        $(this).attr('onclick', 'selectText("' + idja + '")');
        selectcounter++;
    });     
});

function selectText(containerid) {
    if (document.selection) {
        var range = document.body.createTextRange();
        range.moveToElementText(document.getElementById(containerid));
        range.select();
    } else if (window.getSelection) {
        var range = document.createRange();
        range.selectNode(document.getElementById(containerid));
        window.getSelection().addRange(range);
    }
}
.selectable {
    cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
click below text to select it, then drag into bookmarks bar to add as bookmark
<p class="selectable">javascript:(function(){ window.open('https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page=/'+ document.location.href); })();</p>

【问题讨论】:

  • 你不能使用整个document.location.href。您需要提取路径的最后一部分。
  • @mwoelk 我不知道该怎么做,document.location.href 是我唯一真正知道的事情。

标签: javascript wikipedia wiki bookmarklet bookmarks


【解决方案1】:

您可以将其用作书签:

javascript:(function(){ window.open('https://en.wikipedia.org/wiki/Special:CiteThisPage?page='+ document.location.href.substr(document.location.href.lastIndexOf('/') + 1)); })();

【讨论】:

    【解决方案2】:

    使用路径名而不是href,然后将/wiki/替换为空字符串:

    document.location.pathname.replace(/^\/wiki\//, ''))
    

    完整书签:

    javascript:(function(){ window.open('https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page='+ document.location.pathname.replace(/^\/wiki\//, '')); })();
    

    【讨论】:

    • 感谢您向我解释@izem
    猜你喜欢
    • 2011-12-13
    • 2013-12-26
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 2015-05-08
    • 2016-03-27
    • 2016-10-11
    相关资源
    最近更新 更多