Ingress Intel地圖連接工具

在維基百科及其連接到的Geohack上提供Ingress Intel地圖連接

目前為 2020-03-19 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name              Intel Maps Link Provider
// @name:zh-CN        Ingress Intel地图链接工具
// @name:zh-TW        Ingress Intel地圖連接工具
// @namespace         http://cyblocker.com/
// @version           0.3
// @description       Provide Ingress Intel map link to the coordinate information on Wikipedia and Geohack.
// @description:zh-CN 在维基百科及其链接到的Geohack网站上提供Ingress Intel的地图链接
// @description:zh-TW 在維基百科及其連接到的Geohack上提供Ingress Intel地圖連接
// @author            cyblocker
// @supportURL        https://github.com/cyblocker/Intel-Map-Provider/issues
// @match             https://tools.wmflabs.org/geohack/*
// @match             https://*.wikipedia.org/wiki/*
// @grant             none
// ==/UserScript==

(function () {

    function EnsureDDFormat(input) {
        var pattern = /[NSWE]/i;
        if (pattern.test(input) == false) return input;
        console.debug("input: " + input);
        var parts = input.split(/[^\d\w]+/);
        var dd = parseInt(parts[0], 10) + parts[1] / 60 + parts[2] / (60 * 60);

        if (parts[3] == "S" || parts[3] == "W") {
            dd = dd * -1;
        }
        return dd;
    }

    function getIntelUrl() {
        const intelUrlPrefix = "https://intel.ingress.com/intel?ll=";
        var latitude = EnsureDDFormat(document.getElementsByClassName("latitude")[0].innerText);
        var longitude = EnsureDDFormat(document.getElementsByClassName("longitude")[0].innerText);
        var intelUrl = intelUrlPrefix + latitude + "," + longitude;
        return intelUrl;
    }

    function getWikiLinkItem() {
        var linkItem = document.createElement("a");
        linkItem.setAttribute("href", getIntelUrl());
        linkItem.innerHTML = '<img width="17" title="Ingress Intel Map" src="https://upload.wikimedia.org/wikipedia/commons/6/63/Ingress_Logo.png">';
        return linkItem;
    }

    'use strict';

    const GEOHACK_URL_FORMAT = /tools.wmflabs.org\/geohack\/geohack.php?/i;
    const WIKIPEDIA_URL_FORMAT = /wikipedia.org\/wiki/i;

    if (GEOHACK_URL_FORMAT.test(document.URL)) {
        var mapName = "Map";
        if (/language=zh-yue/.test(document.URL)) {
            mapName = "地圖";
        } else if (/language=ja/.test(document.URL)) {
            mapName = "地図";
        } else if (/language=zh/.test(document.URL)) {
            mapName = "地图";
        }
        // Emsamble the table
        var insertItem = document.createElement("tr");
        var serviceName = document.createElement("th");
        serviceName.innerHTML = '<img width="16" alt="Ingress Logo" src="https://upload.wikimedia.org/wikipedia/commons/6/63/Ingress_Logo.png"> Ingress Intel Map';
        serviceName.setAttribute("scope", "row");
        serviceName.setAttribute("style", "font-weight:normal; text-align:left;");
        insertItem.appendChild(serviceName);
        var mapLink = document.createElement("td");
        mapLink.innerHTML = '<a href="' + getIntelUrl() + '">' + mapName + '</a>';
        insertItem.appendChild(mapLink);
        var emptyTd = document.createElement("td");
        insertItem.appendChild(emptyTd);
        insertItem.appendChild(emptyTd);

        // Find correct location and insert
        var tableNode = document.getElementById("GEOTEMPLATE-GLOBAL").getElementsByTagName("tbody")[0];
        var firstItemNode = tableNode.getElementsByTagName("tr")[1];
        tableNode.insertBefore(insertItem, firstItemNode);
    }

    if (WIKIPEDIA_URL_FORMAT.test(document.URL)) {
        if (GEOHACK_URL_FORMAT.test(document.body.innerHTML) == false) return;
        var earthIcons = document.getElementsByClassName("geo-default");
        for (var i = 0; i < earthIcons.length; i++) {
            var parentNode = earthIcons[i].parentNode.parentNode;
            var nextSibling = earthIcons[i].parentNode;
            parentNode.insertBefore(getWikiLinkItem(), nextSibling);
        }
    }

})();