Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send2cgeo button not visible on cache page #62

Closed
wizard4u opened this issue Jan 10, 2020 · 14 comments · Fixed by #64
Closed

send2cgeo button not visible on cache page #62

wizard4u opened this issue Jan 10, 2020 · 14 comments · Fixed by #64
Labels

Comments

@wizard4u
Copy link

sendtogeo not posssible on geocache pages no tab click visible
in chrome and firefox

@wizard4u
Copy link
Author

this is an other issue than cgeo/cgeo#8061
Sorry to be not more clear. On the geocaching.com geocaches the sendtogcgeo is not visible.
I do not know why but probably the tampermonkey script is not working properly

@SammysHP SammysHP transferred this issue from cgeo/cgeo Jan 10, 2020
@Lineflyer
Copy link
Member

I can confirm. Seems the script is broken as there are changes to the website?!
Anyone able to debug this?

@FriedrichFroebel
Copy link
Contributor

FriedrichFroebel commented Jan 12, 2020

I am using Tampermonkey in Firefox 64.0.1 with the latest master branch and cannot reproduce this issue.

@Lineflyer It seems like you just forgot to increase the version numbers for the changed user scripts (#60 and #61 being the recent PRs) and merge the updated versions into the release branch as well.

@Lineflyer
Copy link
Member

@FriedrichFroebel
I tested with Tampermonkey in Firefox 72.01 with latest master as well as release. Its not working for me with both of them. Strange, that it works for you.

I pushed tthe mentioned PRs to master first to give them a test before rolling out the change by merge to release + version number. During these tests I could confirm the problem.

@Lineflyer Lineflyer changed the title sendtogeo not posssible on geocache pages no click send2cgeo button not visible on cache page Jan 12, 2020
@Lineflyer
Copy link
Member

I don't have any javascript expertise.
I do see a failure in the browsers web console "ReferenceError: $ is not defined" linking to

document.getElementsByTagName("head")[0].appendChild(s);

@FriedrichFroebel
Copy link
Contributor

Then we may try to include jQuery for it - this should fix the problem (see https://stackoverflow.com/questions/24753908/trying-to-load-jquery-into-tampermonkey-script). The current jQuery version is 3.4.1. (In my case jQuery seems to be loaded by the GC Little Helper II script, so this does not raise an error.)

@bekuno
Copy link
Member

bekuno commented Jan 12, 2020

With adding jQuery it works on my Firefox.
Will test it deeper in the next days. (The integration in the search result page is buggy, the integration in the new map has to completely rewritten)

@ave72
Copy link

ave72 commented Jan 14, 2020

indeed, added

// @require http://code.jquery.com/jquery-3.4.1.min.js

to send2cgeo script (before all //@include lines) resolved issue in both Firefox (72.0.1 (64-bit)) and Chrome (79.0.3945.117 (Official Build) (64-bit))

@wizard4u
Copy link
Author

Thanx,
Yes is works again
script is:

// ==UserScript==
// @name Send to c:geo
// @namespace http://send2.cgeo.org/
// @description Add button "Send to c:geo" to geocaching.com
// @author c:geo team and contributors
// @grant none
// @require http://code.jquery.com/jquery-3.4.1.min.js
// @include https://www.geocaching.com/play/search*
// @include https://www.geocaching.com/play/search/*
// @include http://www.geocaching.com/seek/cache_details*
// @include https://www.geocaching.com/seek/cache_details*
// @include https://www.geocaching.com/map/*
// @include http://www.geocaching.com/geocache/*
// @include https://www.geocaching.com/geocache/*
// @include http://www.geocaching.com/my/recentlyviewedcaches*
// @include https://www.geocaching.com/my/recentlyviewedcaches*
// @include http://www.geocaching.com/seek/nearest*
// @include https://www.geocaching.com/seek/nearest*
// @ICON https://send2.cgeo.org/send2cgeo.png
// @downloadurl https://github.com/cgeo/send2cgeo/raw/release/send2cgeo.user.js
// @updateURL https://github.com/cgeo/send2cgeo/raw/release/send2cgeo.user.js
// @supportURL https://github.com/cgeo/send2cgeo/issues
// @Version 0.46
// ==/UserScript==

// Inserts javascript that will be called by the s2cgeo button. The closure
// look strange, but avoids having to escape the code. Almost everything
// is put into that script element so that geocaching.com's jQuery may be
// accessed.

/* global s2geomulti */

var s = document.createElement('script');

s.type = 'text/javascript';
s.textContent = '(' + function() {
// function that handles the actual sending //////////////////////////////////

window.s2geo = function(code) {
// show the box and the "please wait" text
$('#send2cgeo, #send2cgeo div').fadeIn();
// hide iframe for now and wait for page to be loaded
$('#send2cgeo iframe')
.hide()
.off('load')
.attr('src', 'https://send2.cgeo.org/add.html?cache='+code)
.on('load', function() {
// hide "please wait text" and show iframe
$('#send2cgeo div').hide();
// hide box after 3 seconds
$(this).css('display', 'block').parent().delay(3000).fadeOut();
});
};
function s2cgeoProgressReport(message,append) {
if (append) {
$("#send2cgeo_controls_div").append(message);
} else {
$("#send2cgeo_controls_div").html(message);
}
}
function s2geomultiLoadMore(requestedCnt, alreadySent, skipFound, moreClicked) {
if (!moreClicked) {
s2cgeoProgressReport("Waiting to load more caches from web site, sent "+alreadySent+" caches.",false);
var loadMoreBtn = $("#loadmore");
loadMoreBtn.click();
moreClicked=true;
}
s2cgeoProgressReport(".",true);
setTimeout(function() {
s2geomulti(requestedCnt, skipFound, moreClicked);
}, 2000);
}

function s2geomultiProcessLine(toAddIframe, skipFound, requestedCnt, alreadySent) {
if (skipFound) {
if (toAddIframe.parent().parent().find("use[xlink\:href*=#icon-found]").length === 0) {
var code = toAddIframe.attr("send2cgeo_gccode");
toAddIframe.html("<iframe width=120 height=80 src="https://send2.cgeo.org/add.html?cache="+code+"\">");
toAddIframe.attr("send2cgeo_sent","1");
s2cgeoProgressReport((alreadySent+1)+" ",alreadySent!==0);
} else {
var trToDel = toAddIframe.parent().parent();
trToDel.remove();
s2cgeoProgressReport(".",alreadySent!==0);
}
}
setTimeout(function() {
s2geomulti(requestedCnt, skipFound, false);
}, 100);
}

window.s2geomulti = function(requestedCnt,skipFound,moreClicked) {
if ( typeof(skipFound) == "undefined" ) {
skipFound = $("#send2cgeo_skip_found").is(":checked");
}
var alreadySent = $("[send2cgeo_sent]").length;
if ( alreadySent < requestedCnt) {
var toAddIframe = $("[send2cgeo_gccode]").not("[send2cgeo_sent]").first();
if (toAddIframe.length) {
s2geomultiProcessLine(toAddIframe, skipFound, requestedCnt, alreadySent);
} else {
s2geomultiLoadMore(requestedCnt, alreadySent, skipFound, moreClicked);
}
} else {
s2cgeoProgressReport("Finished after sending "+alreadySent+" caches.");
}
};

$("#searchResultsTable").before(
"

<a href="#" onclick="window.s2geomulti(50); return false;">Send2cgeo: 50 "
+ "<a href="#" onclick="window.s2geomulti(100); return false;">100 "
+ "<a href="#" onclick="window.s2geomulti(200); return false;">200 "
+ "<a href="#" onclick="window.s2geomulti(500); return false;">500 "
+ "<span class="label">Skip found caches
"
);

// check for premium membership (parts of the page content are different)
function premiumCheck() {
var premium;
if (document.getElementsByClassName('li-membership').length) {
premium = document.getElementsByClassName('li-membership')[0];
} else if (document.getElementsByClassName('li-upgrade').length) {
premium = document.getElementsByClassName('li-upgrade')[0];
} else {
premium = true;
}

  // premium has either an empty <li class="li-upgrade">
  // or none of li-membership / li-upgrade present
  if (premium != true && premium.children.length) {
      // in case GC.com changes the content,
      // it still has to contain only "Upgrade" string
      if (premium.children[0].innerHTML == 'Upgrade') {
          premium = false;
      }
  } else {
      premium = true;
  }
  return premium;

}

// this adds a column with send2cgeo button in search results table
function addSend2cgeoColumn(field) {
var GCCode = $(field).text();
GCCode = GCCode.slice( GCCode.indexOf("|") + 1 ).trim();

    var html = '<td class="mobile-show" >'
        + '<a href="https://send2.cgeo.org/add.html?cache=' + GCCode + '" '
        + "onclick='window.s2geo(\"" + GCCode + "\"); return false;' send2cgeo_gccode='"+GCCode+"'>"
        + '<img height="50" src="https://send2.cgeo.org/send2cgeo.png" '
        + 'border="0"> '
        + '</a></td>';

        $(field).parent().parent().before(html);

}

// waits for new elements (by ajax calls) injected into the DOM and calls a certain
// method for certain elements
// (here: used in search results - these are loaded lazyly when scrolling down)
window.waitForKeyElements = function(selectorTxt, actionFunction, bWaitOnce, iframeSelector) {
var targetNodes, btargetsFound;

if (typeof iframeSelector == "undefined")
    targetNodes = $(selectorTxt);
else
    targetNodes = $(iframeSelector).contents().find (selectorTxt);

if (targetNodes  &&  targetNodes.length > 0) {
    btargetsFound   = true;
    /*--- Found target node(s).  Go through each and act if they
        are new.
    */
    targetNodes.each ( function () {
        var jThis        = $(this);
        var alreadyFound = jThis.data ('alreadyFound')  ||  false;

        if (!alreadyFound) {
            //--- Call the payload function.
            var cancelFound     = actionFunction (jThis);
            if (cancelFound)
                btargetsFound   = false;
            else
                jThis.data ('alreadyFound', true);
        }
    } );
}
else {
    btargetsFound   = false;
}

//--- Get the timer-control variable for this selector.
var controlObj      = waitForKeyElements.controlObj  ||  {};
var controlKey      = selectorTxt.replace (/[^\w]/g, "_");
var timeControl     = controlObj [controlKey];

//--- Now set or clear the timer as appropriate.
if (btargetsFound  &&  bWaitOnce  &&  timeControl) {
    //--- The only condition where we need to clear the timer.
    clearInterval (timeControl);
    delete controlObj [controlKey]
}
else {
    //--- Set a timer, if needed.
    if ( ! timeControl) {
        timeControl = setInterval ( function () {
                waitForKeyElements (selectorTxt, actionFunction,
                    bWaitOnce, iframeSelector);
            },
            300
        );
        controlObj [controlKey] = timeControl;
    }
}
waitForKeyElements.controlObj   = controlObj;

}

window.waitForKeyElements(".cache-details", addSend2cgeoColumn, false);

// Defines the elements to insert into the page //////////////////////////////
var boxWidth = 20,
boxHeight = 7;

var boxStyle = 'display:none; background:#1D1D1D; z-index:1000; left:50%;'
+ 'box-shadow:0 0 0.5em #000; padding:0; border:0; '
+ 'position:fixed; top:0.5em; text-align:center; '
+ 'margin-left:-'+(boxWidth/2)+'em; line-height:'+boxHeight+'em;'
+ 'width:'+boxWidth+'em; height:'+boxHeight+'em; color: #fff';
var waitStyle = 'width: '+boxWidth+'em; color: #fff';
var iframeStyle = 'border:0; width:'+boxWidth+'em; height: '+boxHeight+'em';

$('body').append('

'
+ '
Please wait…
'
+ '<iframe style="'+iframeStyle+'"></iframe>'
+ '
');

// Append to send2cgeo links/buttons /////////////////////////////////////////
var map = document.getElementById('cacheDetailsTemplate');

if(map !== null) {
// geocaching.com map view
var html = 'Log Visit'
+ '<a class="lnk ui-block-b" '
+ 'href="https://send2.cgeo.org/add.html?cache={{=gc}}" '
+ "onclick="window.s2geo('{{=gc}}'); return false;" "
+ 'class="lnk">'
+ '<img src="/images/sendtogps/sendtogps_icon.png" '
+ 'align="absmiddle" border="0"> '
+ 'Send to c:geo';

map.innerHTML = map.innerHTML.replace('Log Visit</span>', html);

} else if(document.getElementById('searchResultsTable') != null){
// geocaching.com new search

// Send 2 cgeo column header for func addSend2cgeoColumn
var S2CGHeader = '<th class="mobile-show"><a class="outbound-link">Send to c:geo</a></th>';
if (premiumCheck()) {
  $("#searchResultsTable th").first().after(S2CGHeader);
  $("#searchResultsTable col").first().after('<col></col>');
} else {
  $("#searchResultsTable th").first().before(S2CGHeader);
  $("#searchResultsTable col").first().before('<col></col>');

}

var caches = $(".cache-details");
caches.each(addSend2cgeoColumn);

} else if(document.getElementById('ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoCode') != null){
// geocaching.com cache detail page
var GCCode = $('#ctl00_ContentBody_CoordInfoLinkControl1_uxCoordInfoCode')
.html();

var html = '<input type="button" '
         + 'value="Send to c:geo" '
         + 'onclick="window.s2geo(\''+GCCode+'\'); '
         + 'return false;" '
         + '/>';

$('#Download p:last').append(html);
$('#Download dd:last').append(html);

} else {
// geocaching.com recentlyviewed
$('img[src="/images/icons/16/send_to_gps.png"]').each(function(){
$(this).attr('alt', "Send to c:geo").attr('title', "Send to c:geo");
});
$('a[title="Send to GPS"]').each(function(){
var text = $(this).parent().parent().find(".Merge").last().find(".small").first().text().split("|");
var GCCode = text[text.length - 2].trim();
this.href="javascript:window.s2geo('"+GCCode+"')";
this.title = "Send to c:geo";
});

}
} + ')();';

// Inject Script. Can't use jQuery yet, because the page is not
// accessible from Tampermonkey
document.getElementsByTagName("head")[0].appendChild(s);

@wizard4u wizard4u reopened this Jan 14, 2020
@wizard4u
Copy link
Author

With the suggested addition it is not possible to open a geocache-page from the map

@bekuno
Copy link
Member

bekuno commented Jan 14, 2020

The implementation for the map is completely broken with the GC new map.
see #63

@wizard4u
Copy link
Author

My point is that with the remedy of av72 the geocaches are not clickable(and therefor will not open in a new tab) any more on the geocache map in chrome. Without the remedy (or not running the script) on the mapping page the caches do open in a new page on clicking

@ave72
Copy link

ave72 commented Jan 15, 2020

I presume there are two issues here:

  1. in the detailed cache page, there was no 'send2cgeo'-button anymore; that is solved with above script.
  2. opening of new browser page when clicking on the map on a cache. That is not solved, but also by disabling it does not return. bekuno is right here, sidebar structure completely changed. In the new structure, you need to click the green, right-pointing arrow in top to open up the detailed cache page.

I would suggest to keep this #62 strictly to the button on the detailed cache page; it then can be closed. Directly opening a new detailed cache page when clicking on the map can then be resolved under #63, I suspect this will have significantly more impact.

@bekuno
Copy link
Member

bekuno commented Jan 16, 2020

My point is that with the remedy of av72 the geocaches are not clickable(and therefor will not open in a new tab) any more on the geocache map in chrome. Without the remedy (or not running the script) on the mapping page the caches do open in a new page on clicking

@wizard4u
Please check if the problem in crome is with the updated script.
If yes please file a new issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants