Skip to content

Commit

Permalink
refactor local stash links on edit pages
Browse files Browse the repository at this point in the history
  • Loading branch information
7dJx1qP committed Oct 22, 2022
1 parent 62dc2d5 commit e2c1e1f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 141 deletions.
3 changes: 2 additions & 1 deletion dist/public/scene.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
}

.stash-performer-link img,
.stash-scene-link img {
.stash-scene-link img,
.stash-studio-link img {
width: 2rem;
padding-left: 0.5rem;
}
Expand Down
193 changes: 53 additions & 140 deletions src/StashDBUserscriptLibrary.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,27 @@
}
return this.callGQL(reqData);
}
async findPerformerByStashId(id) {
const reqData = {
"variables": {
"performer_filter": {
"stash_id": {
"value": id,
"modifier": "EQUALS"
}
}
},
"query": `query FindPerformers($filter: FindFilterType, $performer_filter: PerformerFilterType) {
findPerformers(filter: $filter, performer_filter: $performer_filter) {
count
performers {
id
}
}
}`
}
return this.callGQL(reqData);
}
/*async processScenes(data) {
if (data?.data?.queryScenes?.scenes) {
return Promise.all(data?.data?.queryScenes?.scenes.map(scene => this.processListScene(scene.id)));
Expand Down Expand Up @@ -561,146 +582,43 @@
this.processEdit(sceneEdit);
}
}
addStashLink(node, localId, stashType) {
if (!localId) return;
const localLink = `${this.stashUrl}/${stashType}/${localId}`;
const link = document.createElement('a');
link.classList.add(`stash-${stashType.slice(0, -1)}-link`);
link.href = localLink;
const stashIcon = document.createElement('img');
stashIcon.src = STASH_IMAGE;
link.appendChild(stashIcon);
link.setAttribute('target', '_blank');
insertAfter(link, node);
}
processEdit(sceneEdit) {
const header = sceneEdit.querySelector('.card-header h5');
const [editOp, editType] = header.innerText.split(' ');
console.log('edit', sceneEdit, editOp, editType);
const performerNodes = [];
let sceneStudioNodes = [];
if (editOp === 'Merge') {
if (editType === 'Performer') {
// performer links
const nodes = getElementsByXpath(".//div[@class='card-body']//div[text()='Merge' or text()='Into']/following-sibling::div[@class='col-10']//a", sceneEdit);
let node = null;
while (node = nodes.iterateNext()) {
performerNodes.push(node);
}
}
}
if (editOp === 'Create') {
if (editType === 'Performer') {
// performer link
const scenePerformer = getElementByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'ps-3')]/a", sceneEdit);
performerNodes.push(scenePerformer);
}
else if (editType === 'Scene') {
// performer links
let nodes = getElementsByXpath(".//div[@class='card-body']//div[contains(@class, 'ListChangeRow-Performers')]//li/a", sceneEdit);
let node = null;
while (node = nodes.iterateNext()) {
performerNodes.push(node);
}

// scene/studio link
nodes = getElementsByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'ps-3')]/a", sceneEdit);
while (node = nodes.iterateNext()) {
sceneStudioNodes.push(node);
}

// studio link
const studioNode = getElementByXpath(".//div[@class='card-body']//b[text()='Studio']/following-sibling::div[@class='col-10']//a", sceneEdit);
sceneStudioNodes.push(studioNode);
}
}
else if (editOp === 'Modify') {
if (editType === 'Performer') {
// performer link
const scenePerformer = getElementByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'col')]/a", sceneEdit);
performerNodes.push(scenePerformer);
}
else if (editType === 'Scene') {
// performer links
let nodes = getElementsByXpath(".//div[@class='card-body']//div[contains(@class, 'ListChangeRow-Performers')]//li/a", sceneEdit);
let node = null;
while (node = nodes.iterateNext()) {
performerNodes.push(node);
}

// scene link
nodes = getElementsByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'col')]/a", sceneEdit);
while (node = nodes.iterateNext()) {
sceneStudioNodes.push(node);
}

// studio links
nodes = getElementsByXpath(".//div[@class='card-body']//b[text()='Studio']/following-sibling::div//a", sceneEdit);
while (node = nodes.iterateNext()) {
sceneStudioNodes.push(node);
}
}
}
else if (editOp === 'Destroy') {
if (editType === 'Performer') {
// performer link
const scenePerformer = getElementByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'col')]//a", sceneEdit);
performerNodes.push(scenePerformer);
}
else if (editType === 'Scene') {
// scene link
let nodes = getElementsByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'col')]//a", sceneEdit);
let node = null;
while (node = nodes.iterateNext()) {
sceneStudioNodes.push(node);
}
}
else if (editType === 'Studio') {
// studio link
let nodes = getElementsByXpath(".//div[@class='card-body']/div[contains(@class, 'row')]/div[contains(@class, 'col')]//a", sceneEdit);
let node = null;
while (node = nodes.iterateNext()) {
sceneStudioNodes.push(node);
}
for (const anchor of sceneEdit.querySelectorAll('a')) {
if (anchor.classList.contains('checked-stash')) continue;
anchor.classList.add('checked-stash');
const url = new URL(anchor.href);
if (url.pathname.startsWith('/scenes/')) {
const stashId = url.pathname.replace('/scenes/', '');
this.findSceneByStashId(stashId).then(data => {
const localId = data?.data?.findScenes?.scenes[0]?.id;
this.addStashLink(anchor, localId, 'scenes');
});
}
}

for (const scenePerformer of performerNodes) {
if (scenePerformer && !scenePerformer.parentElement.querySelector('.stash-performer-link')) {
const url = new URL(scenePerformer.href);
else if (url.pathname.startsWith('/performers/')) {
const stashId = url.pathname.replace('/performers/', '');
this.createStashPerformerLink(stashId, function (performerLink) {
scenePerformer.parentElement.appendChild(performerLink);
this.findPerformerByStashId(stashId).then(data => {
const localId = data?.data?.findPerformers?.performers[0]?.id;
this.addStashLink(anchor, localId, 'performers');
});
}
}

for (const sceneNode of sceneStudioNodes) {
console.log('sceneNode', sceneNode);
if (sceneNode && !sceneNode.parentElement.querySelector('.stash-scene-link')) {
const url = new URL(sceneNode.href);
if (url.pathname.startsWith('/scenes/')) {
const stashId = url.pathname.replace('/scenes/', '');
this.findSceneByStashId(stashId).then(data => {
const localId = data?.data?.findScenes?.scenes[0]?.id;
if (localId) {
const localLink = this.stashUrl + '/scenes/' + localId;
const performerLink = document.createElement('a');
performerLink.classList.add('stash-scene-link');
performerLink.href = localLink;
const stashIcon = document.createElement('img');
stashIcon.src = STASH_IMAGE;
performerLink.appendChild(stashIcon);
performerLink.setAttribute('target', '_blank');
insertAfter(performerLink, sceneNode);
}
});
}
else {
const stashId = url.pathname.replace('/studios/', '');
this.findStudioByStashId(stashId).then(data => {
const localId = data?.data?.findStudios?.studios[0]?.id;
if (localId) {
const localLink = this.stashUrl + '/studios/' + localId;
const performerLink = document.createElement('a');
performerLink.classList.add('stash-studio-link');
performerLink.href = localLink;
const stashIcon = document.createElement('img');
stashIcon.src = STASH_IMAGE;
performerLink.appendChild(stashIcon);
performerLink.setAttribute('target', '_blank');
insertAfter(performerLink, sceneNode);
}
});
}
else if (url.pathname.startsWith('/studios/')) {
const stashId = url.pathname.replace('/studios/', '');
this.findStudioByStashId(stashId).then(data => {
const localId = data?.data?.findStudios?.studios[0]?.id;
this.addStashLink(anchor, localId, 'studios');
});
}
}
}
Expand Down Expand Up @@ -815,22 +733,17 @@
});
}
else if (stashType === 'users' && stashId && action === 'edits') {
console.log('user edits');
waitForElementByXpath("//div[contains(@class, 'EditCard')]|//h4[text()='No results']", (xpath, el) => {
this.processEdits();
});
}
else if (stashType === 'edits') {
if (stashId) {
// specific edit page
console.log('edit page');
waitForElementByXpath("//div[contains(@class, 'EditCard')]|//h4[text()='No results']", (xpath, el) => {
this.processEdits();
});
}
else {
// all edits page
console.log('edits page');
waitForElementByXpath("//div[contains(@class, 'EditCard')]|//h4[text()='No results']", (xpath, el) => {
this.processEdits();
});
Expand Down

0 comments on commit e2c1e1f

Please sign in to comment.