-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
46 lines (41 loc) · 2.19 KB
/
background.js
File metadata and controls
46 lines (41 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/***************************************************************************************************************************************************************
* COPYRIGHT (C) 2025 HTTPS://GITHUB.COM/ALIREZAABDI01 *
* THIS SOFTWARE IS PROPRIETARY. *
* SOURCE CODE IS MADE AVAILABLE FOR TRANSPARENCY ONLY. YOU MAY NOT COPY, MODIFY, REDISTRIBUTE, OR CREATE DERIVATIVE WORKS WITHOUT EXPRESS WRITTEN PERMISSION. *
* ALL RIGHTS RESERVED. *
***************************************************************************************************************************************************************/
chrome.runtime.onInstalled.addListener(() => {
// Create the context menu item
chrome.contextMenus.create({
id: "rtlTextFormatter",
title: "Format text as RTL",
contexts: ["selection"], // Only available when text is selected
});
});
// When the menu item is clicked
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "rtlTextFormatter") {
// Send a message to the content script to apply RTL direction to the parent
chrome.scripting.executeScript({
target: { tabId: tab.id },
func: applyRTLToParent,
});
}
});
// Function to apply RTL direction to the parent element of the selected text
function applyRTLToParent() {
const selection = window.getSelection();
if (selection.rangeCount > 0) {
const selectedNode = selection.getRangeAt(0).startContainer;
// Traverse up the DOM tree to find the closest parent element
let parentElement = selectedNode;
console.log(parentElement);
while (parentElement && parentElement.nodeType !== 1) {
parentElement = parentElement.parentNode;
}
// Apply the RTL direction to the parent element
if (parentElement) {
parentElement.style.direction = 'rtl';
}
}
}