Forge: Select a single part: cant switch between parts - javascript

I am trying to link part id's to PDF pages. At the moment I am test linking them by:
const page = results[0].selection[0] % 100;
Each part ID does now link to a page. However, it only works when I click on a part -> click off the part -> then click on another part. If you try to switch between parts it doesnt show the pdf.
I think there is something wrong with the pdf#page part. As I can switch between parts if I change the code and dont link to a page.
I noticed that the same issue happens on the demo: https://forge-digital-twin.autodesk.io/
Any ideas on how to solve this?
Many thank,
Poppy
Here is my code:
function initDocument(facility) {
// Only enable when exactly one part is selected
const $alert = $('#maintenance-docs div.alert');
$alert.show();
$('#maintenance-docs embed').hide()
$('#maintenance-docs > .show-based-on-selection').hide();
NOP_VIEWER.addEventListener(Autodesk.Viewing.AGGREGATE_SELECTION_CHANGED_EVENT, function(ev) {
const results = NOP_VIEWER.getAggregateSelection();
if (results.length === 1 && results[0].selection.length === 1) {
$alert.hide();
const page = results[0].selection[0] % 100;
console.log(page);
$('#maintenance-docs > .show-based-on-selection').show();
$('#maintenance-docs embed').attr('src', `/resources/Operation-manual-Sigg-Plant.pdf#page=${page}`).show();
//$('#maintenance-docs embed').show();
} else {
$alert.show();
$('#maintenance-docs > .show-based-on-selection').hide();
$('#maintenance-docs embed').attr('src', '');
//$('#maintenance-docs embed').hide();
}
});
}

Try with
Autodesk.Viewing.SELECTION_CHANGED_EVENT
Inside the event function keep same functionality.

Related

How do I make external links pass through an affiliate link first using .htaccess or Javascript?

I have a significant amount of external links on my website in this format:
website.com/product/[variable]
I need these links to somehow pass through “myaffiliatelink.com” before being redirected to website.com/product/[variable].
Is this possible using .htaccess or Javascript?
I looked into using .htaccess, but seems I would need to do this individually. Is there a way to set a rule such that any external link using "website.com/product/[variable]" should pass through "myaffiliatelink.com" first?
// catch every click on the page
document.addEventListener("click", e => {
const target = e.target;
if (target.tagName === 'A' && target.href.indexOf("website.com") !== -1) {
// prevent the <a> tag from navigating
e.preventDefault();
const lastSlash = target.href.lastIndexOf('/');
if (lastSlash > 0) {
const variable = target.href.substring(lastSlash + 1);
// use the commented code below, or a window.open
//location.href = "https://myaffiliatelink.com/"+variable;
// for demonstration
console.log("https://myaffiliatelink.com/" + variable);
}
}
})
Product
<p>should pass through "myaffiliatelink.com"</p>

How to simulate a mouse click on an anchor tag

I'm writing a chrome extension and from that extension i want to click on a particular anchor tag in a webpage on a particular website.
let helpRequestButton = document.querySelectorAll(".live-doubt-item-resolve.ng-scope a");
Using this query selector i'm able to get the anchor tag from the web page which looks like.
<a data-ga-label="resolve_now_live" data-ga-action="clicked" ng-if="helpRequestObj.item_type === 'Topic'" ng-click="claimLiveRequest($event, true, helpRequestObj.id)" target="_blank" class="content-heading ng-scope" data-ga-category="HelpRequestDashboardLive"> </a>
I just want to click on this tag using java script.
But helpRequestButton.click() dosent work.
I have tried the same solution with jQuery(did'nt work).
Also i tried to search a lot on web on how to simulate a mouse click and none of the solutions worked uptill now.
Just for refrence i'm giving the code snippet of my extension what chrome would call one the webpage is loaded. The button which i have to click is present on the web page.
let timeOut;
function stopTimeout() {
clearTimeout(timeOut);
}
function checkIncomingHelpRequests() {
let helpRequestButton = document.querySelectorAll(".live-doubt-item-resolve.ng-scope a");
console.log(helpRequestButton[0]);
if (helpRequestButton.length > 0) {
> ***// This is the place i want to click the button.***
// helpRequestButton.
//helpRequestButton.click();
}
}
function selectTag() {
// var object = document.querySelector(".main-class");
// console.log(object);
let liButton = document.querySelectorAll(".pointer.tab.ga-event-tracker")[0];
//let liButton = $("li.pointer.tab.ga-event-tracker")[0];
//console.log(liButton);
if (liButton !== undefined) {
liButton.click();
checkIncomingHelpRequests();
}
timeOut = setTimeout("selectTag()", timeOutInterval);
}
$(document).ready(function () {
selectTag();
});
Please help me. Thanks in advance.

Prepending Old Chat Messages to Chat Box

I'm trying to prepend old messages to a chatbox when the user scrolls to the top. I'm using eventListeners as the code illustrates, but I'm running into an error where only the last chatbox is working properly. It seems that bodies[index].scrollTop and bodies[index].scrollHeight always returns 0 with the exception of bodies[lastIndex]. Why might this be? (logging bodies[index] correctly returns the div element)
document.querySelectorAll('.popup-messages').forEach((item, index) =>
{
item.addEventListener('scroll', async function()
{
if (chatReady[index] == true && bodies[index].scrollTop == 0)
{
chatReady[index] = false;
var previousHeight = bodies[index].scrollHeight;
await getMessages(item.id.replace(":body", ""));
var heightDiff = bodies[index].scrollHeight - previousHeight;
bodies[index].scrollTop += heightDiff;
}
})
})
Edit: If there's a different way to make multiple eventListeners dynamically, please share it with me as it would help a lot.
This problem was solved by replacing bodies[index] with item. Perhaps there was a bug that occurs when more than one variable is associated with an element.

Change all website links to affiliate links automatically

I would like to be able to automatically change links into affiliate links automatically on my MediaWiki installation. This would help to reduce the amount of time managing links in case the code needs to be changed in the future.
This is the setup of my GOG.com affiliate scheme: I need to append this key to the end of every GOG.com link: ?pp=708a77db476d737e54b8bf4663fc79b346d696d2
gog.com/en/gamecard/baldurs_gate_the_original_saga/?pp=708a77db476d737e54b8bf4663fc79b346d696d2
Is it possible for a piece of code, like Javascript, to intercept all links (like http://www.gog.com/en/gamecard/baldurs_gate_the_original_saga/) and append the affiliate code on the end, as in the above example?
I'm aware of this piece of Javascript code called Amazon Associate Link Localiser which does a similar thing. However, it only works for Amazon links, and it also localises links which is a feature I don't want.
Right way is to use LinkerMakeExternalLink mediawiki hook like that ( you can put it at bottom of your LocalSettings.php:
$wgHooks['LinkerMakeExternalLink'][] = 'ExampleExtension::exampleLinkerMakeExternalLink';
class ExampleExtension {
public static function exampleLinkerMakeExternalLink( &$url, &$text, &$link, &$attribs, $linktype ) {
if( strpos( $url, 'gog.com') !== false ) {
$url .= '?pp=708a77db476d737e54b8bf4663fc79b346d696d2';
}
return false;
}
}
Not sure how great it would be performance wise for hundreds of links.
// Plain Javascript
var links = document.getElementsByTagName('a');
for (var i = 0, max = links.length; i < max; i++) {
var _href = links[i].href;
if (_href.indexOf('gog.com') !== -1) {
links[i].href = _href + '?pp=708a77db476d737e54b8bf4663fc79b346d696d2';
}
}
DEMO
So you can also use jquery to bind any link click. This way you can do your link eval on the fly. This jsfiddle is a rough run through of what i think you're trying to accomplish. The alerts are just for your benefit and should be removed.
$("a").click(function() {
addAffiliate(this);
});
myCode = "?pp=708a77db476d737e54b8bf4663fc79b346d696d2";
myAmazonCode = "?tag=shihac-20"
function addAffiliate(link) {
alert("enterting script: " + link.href);
if ((link.href).indexOf("gog.com") > -1 && (link.href).indexOf(myCode) < 0) {
link.href = link.href + myCode;
}else if((link.href).indexOf("amazon.com") > -1 && (link.href).indexOf(myAmazonCode) < 0){
link.href = link.href + myAmazonCode;
}
alert(link.href);
return true;
}​
http://jsfiddle.net/du47b/23/
UPDATE: added code and fully qualified paths
UPDATE: added 'else if' block for other codes. using 'else if' instead of just another if block will hopefully cut back on unnecessary processing.

sliderman.js - call "go" function from href

I'm using sliderman.js (http://www.devtrix.net/sliderman/api.html).
The last couple hours I tried to figure out how to call a specific slide from an .
Test but I always get the message that this function is not defined.
Any help is appreciated.
There seems to be a method called Slider.go which is in the file "sliderman.1.3.0.js
Slider.go = function(index){
index = (images.length + index) % images.length;
autoplay(false);
if(status != 'free' || current == index) return autoplay(true) && false;
previous = current;
current = index;
eventCall('loading');
showLoading(true);
if(contentmode) doEffect(images[current]);
else loadImage(images[current], doEffect, display.always_show_loading);
return true;
}//go
You were almost there you need to call .go on the variable you use to initialize the slider. For example Demo 3 uses,
var demoSlider_3 = Sliderman.slider({container: 'SliderName_3', width: 800, height: 200, effects: effectsDemo3, display: {autoplay: 3000}});
So to get it to work on a link for example you can do the following.
go
the number 1 is the id of the slide. I went ahead and tested it locally and it worked every time. You can try it by downloading Demo 3, and adding the link anywhere on the page.

Categories